Package java.lang.ref

Examples of java.lang.ref.ReferenceQueue$Null


  /**
   * @tests java.lang.ref.PhantomReference#get()
   */
  public void test_get() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(false);
    PhantomReference pr = new PhantomReference(bool, rq);
    assertNull("Same object returned.", pr.get());
  }
View Full Code Here


  /**
   * @tests 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);
View Full Code Here

  public void test_Constructor() {
    assertTrue("Used for testing.", true);
  }

  protected void setUp() {
    rq = new ReferenceQueue();
  }
View Full Code Here

  /**
   * @tests java.lang.ref.SoftReference#SoftReference(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 {
      SoftReference sr = new SoftReference(bool, rq);
      assertTrue("Initialization failed.", ((Boolean) sr.get())
          .booleanValue());
View Full Code Here

  /**
   * @tests java.lang.ref.WeakReference#WeakReference(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 {
      // Allow the finalizer to run to potentially enqueue
      WeakReference wr = new WeakReference(bool, rq);
      assertTrue("Initialization failed.", ((Boolean) wr.get())
View Full Code Here

   * @tests java.lang.ref.Reference#clear()
   */
  public void test_clear() {
    tmpA = new Object();
    tmpB = new Object();
    SoftReference sr = new SoftReference(tmpA, new ReferenceQueue());
    WeakReference wr = new WeakReference(tmpB, new ReferenceQueue());
    assertTrue("Start: Object not cleared.", (sr.get() != null)
        && (wr.get() != null));
    sr.clear();
    wr.clear();
    assertTrue("End: Object cleared.", (sr.get() == null)
View Full Code Here

  /**
   * @tests java.lang.ref.Reference#enqueue()
   */
  public void test_enqueue() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    assertTrue("Enqueue failed.", (!ref.isEnqueued())
        && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued.", rq.poll().get() == obj);
    // This fails...
    assertTrue("Should remain enqueued.", !ref.isEnqueued());
    assertTrue("Can not enqueue twice.", (!ref.enqueue())
        && (rq.poll() == null));

    rq = new ReferenceQueue();
    obj = new Object();
    ref = new WeakReference(obj, rq);
    assertTrue("Enqueue failed2.", (!ref.isEnqueued())
        && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
    assertTrue("Should remain enqueued2.", !ref.isEnqueued()); // This
    // fails.
    assertTrue("Can not enqueue twice2.", (!ref.enqueue())
        && (rq.poll() == null));
  }
View Full Code Here

      protected void finalize() {
        finalized = true;
      }
    }

    final ReferenceQueue rq = new ReferenceQueue();

    class TestThread extends Thread {
      public void run() {
        // Create the object in a separate thread to ensure it will be
        // gc'ed
        Object testObj = new TestObject();
        wr = new WeakReference(testObj, rq);
        testObj = null;
      }
    }

    Reference ref;

    try {
      Thread t = new TestThread();
      t.start();
      t.join();
      System.gc();
      System.runFinalization();
      ref = rq.remove();
      assertTrue("Unexpected ref1", ref == wr);
      assertNotNull("Object not garbage collected1.", ref);
      assertNull("Object could not be reclaimed1.", wr.get());
    } catch (InterruptedException e) {
      fail("InterruptedException : " + e.getMessage());
    }

    try {
      Thread t = new TestThread();
      t.start();
      t.join();
      System.gc();
      System.runFinalization();
      ref = rq.poll();
      assertTrue("Unexpected ref2", ref == wr);
      assertNotNull("Object not garbage collected.", ref);
      assertNull("Object could not be reclaimed.", ref.get());
      // Reference wr so it does not get collected
      assertNull("Object could not be reclaimed.", wr.get());
View Full Code Here

   * @tests java.lang.ref.Reference#get()
   */
  public void test_get() {
    // SM.
    obj = new Object();
    Reference ref = new WeakReference(obj, new ReferenceQueue());
    assertTrue("Get succeeded.", ref.get() == obj);
  }
View Full Code Here

  /**
   * @tests java.lang.ref.Reference#isEnqueued()
   */
  public void test_isEnqueued() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    assertTrue("Should start off not enqueued.", !ref.isEnqueued());
    ref.enqueue();
    assertTrue("Should now be enqueued.", ref.isEnqueued());
    ref.enqueue();
    assertTrue("Should still be enqueued.", ref.isEnqueued());
    rq.poll();
    // This fails ...
    assertTrue("Should now be not enqueued.", !ref.isEnqueued());
  }
View Full Code Here

TOP

Related Classes of java.lang.ref.ReferenceQueue$Null

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.