// Now verify that custom level instances are correctly // garbage collected when no longer referenced
ReferenceQueue<Level> queue = new ReferenceQueue<>();
List<CustomLevelReference> refs = new ArrayList<>();
List<CustomLevelReference> customRefs = new ArrayList<>(); int otherLevels = 0; while (!levels.isEmpty()) {
Level l = levels.stream().findAny().get(); boolean isCustomLoader = isCustomLoader(l); if (isCustomLoader) otherLevels++;
CustomLevelReference ref = new CustomLevelReference(l, queue); if (isCustomLoader) {
customRefs.add(ref);
} else {
refs.add(ref);
}
// remove strong references to l
levels.remove(l);
l = null;
// Run gc and wait for garbage collection if (otherLevels == otherLevelCount) { if (customRefs.size() != otherLevelCount) { thrownew RuntimeException("Test bug: customRefs.size() != "
+ otherLevelCount);
}
waitForGC(customRefs, queue);
}
} if (otherLevelCount != otherLevels || otherLevelCount == 0) { thrownew RuntimeException("Test bug: "
+ "no or wrong count of levels loaded from custom loader");
} if (!customRefs.isEmpty()) { thrownew RuntimeException( "Test bug: customRefs.size() should be empty!");
} while (!refs.isEmpty()) { final Reference<?> ref = refs.remove(0); if (ref.get() == null) { thrownew RuntimeException("Unexpected garbage collection for "
+ ref);
}
}
}
privatestaticvoid waitForGC(List<CustomLevelReference> customRefs,
ReferenceQueue<Level> queue) throws InterruptedException
{ while (!customRefs.isEmpty()) {
Reference<? extends Level> ref2; do {
System.gc(); Thread.sleep(100);
} while ((ref2 = queue.poll()) == null);
staticvoid checkCustomLevel(Level level, Level expected) { // Level value must be the same if (!level.equals(expected)) { thrownew RuntimeException(formatLevel(level) + " != "
+ formatLevel(expected));
}
// Level.parse is expected to return the custom Level if (level != expected) { thrownew RuntimeException(formatLevel(level) + " != "
+ formatLevel(expected));
}
final ResourceBundle rb = getResourceBundle(level);
String name = rb.getString(level.getName()); if (!level.getLocalizedName().equals(name)) { // must have the same localized name thrownew RuntimeException(level.getLocalizedName() + " != " + name);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.