Code example for PhantomReference

Methods: sleep

0
	} 
	 
	@Test 
	public void testPhantomRefQueue() throws InterruptedException { 
		final ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
		PhantomReference<Object> phantomRef = new PhantomReference<Object>(new Object(), queue);
		 
		new Thread(new Runnable() {
			public void run() { 
				try { 
					System.out.println("Awaiting for GC");
					// This will block till it is GCd 
					PhantomReference<Object> ref = (PhantomReference<Object>) queue.remove();
					System.out.println(ref);
					System.out.println("Referenced GC'd");
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
			} 
		}).start();
		 
		// Wait for 2nd thread to start 
		Thread.sleep(2000);
		 
		System.out.println("Invoking GC");
		System.gc();
	}