publicstaticvoid main(String args[]) throws Exception { for (int i=0; i < 5; i++) { new WeakRef().run(); new ExplicitEnqueue().run();
}
System.out.println("Test passed.");
}
staticclass WeakRef { final ReferenceQueue<Object> queue = new ReferenceQueue<Object>(); final Reference<Object> ref; finalint iterations = 1000;
WeakRef() { this.ref = new WeakReference<Object>(new Object(), queue);
}
void run() throws InterruptedException { boolean enqueued = false;
System.gc(); for (int i = 0; i < iterations; i++) {
System.gc();
enqueued = (queue.remove(100) == ref); if (enqueued) break;
}
if (!enqueued) { // GC have not enqueued refWeak for the timeout period thrownew RuntimeException("Error: reference not enqueued");
}
if (ref.enqueue() == true) { // enqueue() should return false since // ref is already enqueued by the GC thrownew RuntimeException("Error: enqueue() returned true;"
+ " expected false");
}
}
}
staticclass ExplicitEnqueue { final ReferenceQueue<Object> queue = new ReferenceQueue<>(); final List<Reference<Object>> refs = new ArrayList<>(); finalint iterations = 1000;
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.