1 import java.lang.ref.*;
2 import java.util.HashSet;
5 class OtherThread extends Thread {
6 public volatile boolean quitNow = false;
7 private final ReferenceQueue q;
8 private final HashSet h;
9 public OtherThread(ReferenceQueue q, HashSet h) {
16 MyRef r = (MyRef) q.remove();
18 System.out.println("Integer: " + Integer.toString(r.val));
19 } catch (InterruptedException e) {
25 class MyRef extends WeakReference {
27 MyRef(Object o, ReferenceQueue q, int val) {
34 System.out.println("This should print a long list of Integers if weak references are working.");
35 ReferenceQueue q = new ReferenceQueue();
36 HashSet h = new HashSet();
37 OtherThread t = new OtherThread(q, h);
39 for (int i=0; i<1000000; i++) {
40 Object o = new Integer(i);
41 Reference r = new MyRef(o, q, i);
44 Runtime.getRuntime().gc();
50 } catch (InterruptedException e) {
54 public static void main(String[] args) {
60 * These are local overrides for various environment variables in Emacs.
61 * Please do not remove this and leave it at the end of the file, where
62 * Emacs will automagically detect them.
63 * ---------------------------------------------------------------------
70 * vim:noexpandtab:sw=4:ts=4: