Search in sources :

Example 16 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project roboguice by roboguice.

the class WeakKeySetUtils method awaitFullGc.

public static void awaitFullGc() {
    // GcFinalization *should* do it, but doesn't work well in practice...
    // so we put a second latch and wait for a ReferenceQueue to tell us.
    ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
    WeakReference ref = new WeakReference<Object>(new Object(), queue);
    GcFinalization.awaitFullGc();
    try {
        assertSame("queue didn't return ref in time", ref, queue.remove(5000));
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) WeakReference(java.lang.ref.WeakReference)

Example 17 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class PhantomReferenceTest method test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue.

/**
     * java.lang.ref.PhantomReference#PhantomReference(java.lang.Object,
     *        java.lang.ref.ReferenceQueue)
     */
public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(true);
    try {
        PhantomReference pr = new PhantomReference(bool, rq);
        // Allow the finalizer to run to potentially enqueue
        Thread.sleep(1000);
        assertTrue("Initialization failed.", !pr.isEnqueued());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    // 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);
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference)

Example 18 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class PhantomReferenceTest method test_get.

/**
     * 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());
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference)

Example 19 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class ReferenceQueueTest method test_Constructor.

/**
     * java.lang.ref.ReferenceQueue#ReferenceQueue()
     */
public void test_Constructor() {
    ReferenceQueue rq = new ReferenceQueue();
    assertNull(rq.poll());
    try {
        rq.remove(100L);
    } catch (InterruptedException e) {
        fail("InterruptedException was thrown.");
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue)

Example 20 with ReferenceQueue

use of java.lang.ref.ReferenceQueue in project robovm by robovm.

the class ReferenceQueueTest method test_removeJ.

/**
     * java.lang.ref.ReferenceQueue#remove(long)
     */
public void test_removeJ() {
    try {
        assertNull("Queue should be empty. (poll)", rq.poll());
        assertNull("Queue should be empty. (remove(1))", rq.remove((long) 1));
        Thread ct = new Thread(new ChildThread());
        ct.start();
        Reference ret = rq.remove(0L);
        assertNotNull("Delayed remove failed.", ret);
    } catch (InterruptedException e) {
        fail("InterruptedExeException during test : " + e.getMessage());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    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.");
    } catch (InterruptedException e1) {
        fail("InterruptedException was thrown.");
    }
    rq = new ReferenceQueue();
    isThrown = false;
    assertNull(rq.poll());
    class RemoveThread extends Thread {

        public void run() {
            try {
                rq.remove(1000L);
            } catch (InterruptedException ie) {
                isThrown = true;
            }
        }
    }
    RemoveThread rt = new RemoveThread();
    rt.start();
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    rt.interrupt();
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    assertTrue(isThrown);
    assertNull(rq.poll());
    try {
        rq.remove(-1);
        fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException iae) {
    //expected
    } catch (InterruptedException e) {
        fail("Unexpected InterruptedException.");
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Aggregations

ReferenceQueue (java.lang.ref.ReferenceQueue)30 WeakReference (java.lang.ref.WeakReference)15 PhantomReference (java.lang.ref.PhantomReference)11 Reference (java.lang.ref.Reference)10 SoftReference (java.lang.ref.SoftReference)9 URLClassLoader (java.net.URLClassLoader)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ClassLoaders.inMemoryClassLoader (org.mockitoutil.ClassLoaders.inMemoryClassLoader)2 SideEffect (dalvik.annotation.SideEffect)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Field (java.lang.reflect.Field)1 SocketPermission (java.net.SocketPermission)1 MarshalledObject (java.rmi.MarshalledObject)1 Remote (java.rmi.Remote)1 RemoteException (java.rmi.RemoteException)1 AccessControlContext (java.security.AccessControlContext)1