Java 8 PhantomReference - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 PhantomReference class.

This code example shows how to use the following methods: sleep, isEnqueued
	 *        java.lang.ref.ReferenceQueue) 
	 */ 
	public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() throws Exception { 
		ReferenceQueue rq = new ReferenceQueue();
		bool = new Boolean(true);
                PhantomReference pr = new PhantomReference(bool, rq);
                // Allow the finalizer to run to potentially enqueue 
                Thread.sleep(1000);
                assertTrue("Initialization failed.", !pr.isEnqueued());
 
                // need a reference to bool so the jit does not optimize it away 
		assertTrue("should always pass", bool.booleanValue());
 
		boolean exception = false;
		try { 
			new PhantomReference(bool, null);
		} catch (NullPointerException e) {
			exception = true;
		} 
		assertTrue("Should not throw NullPointerException", !exception);
Full Snippet Info
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: get, clear, enqueue
     * java.lang.ref.PhantomReference#get() 
     */ 
    public void test_get() { 
        ReferenceQueue rq = new ReferenceQueue();
        bool = new Boolean(false);
        PhantomReference pr = new PhantomReference(bool, rq);
        assertNull("get() should return null.", pr.get());
        pr.enqueue();
        assertNull("get() should return null.", pr.get());
        pr.clear();
        assertNull("get() should return null.", pr.get());
    } 
 
    /** 
     * java.lang.Runtime#gc() 
     */ 
    public void test_gcInteraction() { 
        class TestPhantomReference<T> extends PhantomReference<T> {
            public TestPhantomReference(T referent,
                    ReferenceQueue<? super T> q) {
Full Snippet Info
6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: sleep, enqueue
        Object obj = new Object(); 
        WeakReference wr = new WeakReference(obj, rq);
        Boolean b = new Boolean(true);
        SoftReference sr = new SoftReference(b, rq);
        String str = "Test";
        PhantomReference pr = new PhantomReference(str, rq);
 
        pr.enqueue();
        wr.enqueue();
        sr.enqueue();
 
        try { 
            Reference result = rq.remove(1L);
            assertTrue((Boolean)result.get());
            result = rq.remove(1L);
            assertEquals(obj, result.get());
            result = rq.remove(1L);
            assertNull(result.get());
        } catch (IllegalArgumentException e1) {
            fail("IllegalArgumentException was thrown.");

6
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: clear, enqueue
    } 
 
    public void close() { 
        if (cleanupRef != null)
        { 
            cleanupRef.clear();
            cleanupRef.enqueue();
            cleanupRef = null;
        } 
    } 
 
    String getPortalName() {
        return portalName;
    } 
 
    byte[] getEncodedPortalName() { 
        return encodedName;
    } 
 
    SimpleQuery getQuery() { 
Full Snippet Info
5
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
This code example shows how to use the following methods: get, isEnqueued
		}; 
 
		HashMap map = new HashMap();
		Reference reference = null;
		System.out.println("Testing PhantomReference.");
		reference = new PhantomReference(object, referenceQueue);
 
		map.put(reference, data);
 
		System.out.println(reference.get()); // null
		System.out.println(map.get(reference)); // Data
		System.out.println(reference.isEnqueued()); // false
 
		System.gc();
		System.out.println(reference.get()); // null
		System.out.println(map.get(reference)); // Data
		System.out.println(reference.isEnqueued()); // false
 
		object = null;
		data = null;

5
CodeRank
Stop searching for code, let great code find you!  Add Codota to your java IDE
See Code Examples for Java 8 PhantomReference Methods: