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);
}
}
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);
}
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());
}
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.");
}
}
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.");
}
}
Aggregations