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);
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
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) {
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
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
Contextual code suggestions in your IDE  Get Codota for Java
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() { 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
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
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:clear, enqueue
    } 
 
    void unprepare() { 
        if (cleanupRef != null)
        { 
            cleanupRef.clear();
            cleanupRef.enqueue();
            cleanupRef = null;
        } 
 
        statementName = null;
        encodedStatementName = null;
        fields = null;
        portalDescribed = false;
        statementDescribed = false;
    } 
 
    private final String[] fragments;
    private final ProtocolConnectionImpl protoConnection;
    private String statementName;
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
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);
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:isEnqueued
    } 
 
    public static void main(String[] args) throws Exception {
        ThreadLocalKeyValue target = new ThreadLocalKeyValue();
        ReferenceQueue queue = new ReferenceQueue();
        PhantomReference ref = new PhantomReference(target, queue);
 
        target.add("foo", "bar");
        target = null;
 
        System.gc();
        System.out.println("Released?: " + ref.isEnqueued());
   } 
} 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:get
	@Override 
	public V get(K key) {
		V result = null;
		Reference<V> reference = softMap.get(key);
		if (reference != null) {
			result = reference.get();
		} 
		return result;
	} 
 
	@Override 
	public boolean put(K key, V value) {
		softMap.put(key, createReference(value));
		return true; 
	} 
 
	@Override 
	public void remove(K key) {
		softMap.remove(key);
	} 
7
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:get
public abstract class SoftLazyValue<T> { 
  private SoftReference<T> myReference;
 
  public T getValue() { 
    T t;
    if (myReference == null || (t = myReference.get()) == null) {
      t = compute();
      myReference = new SoftReference<T>(t);
    } 
    return t;
  } 
 
  @NotNull 
  protected abstract T compute(); 
} 
8
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
See Code Examples for Java 8 PhantomReference Methods: