Code example for ReferenceQueue

Methods: poll, remove, sleep

0
        } 
        RemoveThread rt = new RemoveThread();
        rt.start();
        try { 
            Thread.sleep(100);
        } catch(InterruptedException ie) {
             
        } 
        rt.interrupt();
        try { 
            Thread.sleep(100);
        } catch(InterruptedException ie) {
             
        }         
        assertTrue(isThrown);
        assertNull(rq.poll());
    } 
 
    /** 
     * @tests java.lang.ref.ReferenceQueue#remove(long) 
     */ 
    @TestTargetNew( 
        level = TestLevel.COMPLETE, 
        notes = "", 
        method = "remove", 
        args = {long.class} 
    ) 
    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());