Code example for PhantomReference

Methods: sleep

0
		 * meant to be used to access the object, but as a signal that the 
		 * object has already been finalized, and the garbage collector is 
		 * ready to reclaim its memory. 
		 */ 
		final ReferenceQueue<Stuff> queue = new ReferenceQueue<Stuff>();
		PhantomReference<Stuff> phantomS = new PhantomReference<Stuff>(instantiateStuff("Phantom Reachable"), queue);
 
		/* 
		 * I found the object is not enqueued unless I remove the override 
		 * finalize() method from Stuff class. 
		 *  
		 * But why??? 
		 */ 
		new Thread(new Runnable() {
			public void run() { 
				try { 
					System.out.println("Awaiting for GC");
					PhantomReference<Stuff> ref = (PhantomReference<Stuff>) queue.remove();
					System.out.println(ref);
					System.out.println("Stuff has been enqueued!");
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
			} 
		}).start();
		 
		Thread.sleep(2000);
 
		System.out.println("Invoking GC");
		System.gc();
	}