Package java.lang.ref

Examples of java.lang.ref.PhantomReference


    }
   
    public void testCanUseDifferentMapImplementation() throws InterruptedException {
        String value = new String("value");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(value, refQueue);
       
        Map map = new WeakCache(new TreeMap());
        map.put("key", value);
        value = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
        assertEquals(0, map.size());
View Full Code Here


     * @param interceptor    ignored
     * @param connectionInfo the connection that was released
     * @param action         ignored
     */
    public void handleReleased(final ConnectionTrackingInterceptor interceptor, final ConnectionInfo connectionInfo, final ConnectionReturnAction action) {
        final PhantomReference phantomReference = references.remove(connectionInfo.getManagedConnectionInfo());
        if (phantomReference != null) {
            phantomReference.clear();
        }
    }
View Full Code Here

   * @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

   *        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);
  }
View Full Code Here

     * @param obj the object on which to wait
     */
    public WaitUnreachable(Object obj) {
        this.obj = obj;
        queue = new ReferenceQueue();
        ref = new PhantomReference(obj, queue);
    }
View Full Code Here

        try {
            synchronized (monitor) {
                if (display == null) {
                    return null;
                }
                phantomImage = new PhantomReference(display, refQueue);
                String icon = "images/duke.gif";
                bgImg = ImageIO.read(getClass().getResourceAsStream(icon));
                mt = new MediaTracker(this);
                mt.addImage(bgImg, hashCode());
                mt.addImage(display, hashCode());
View Full Code Here

     * @param interceptor    ignored
     * @param connectionInfo the connection that was released
     * @param action         ignored
     */
    public void handleReleased(final ConnectionTrackingInterceptor interceptor, final ConnectionInfo connectionInfo, final ConnectionReturnAction action) {
        final PhantomReference phantomReference = references.remove(connectionInfo.getManagedConnectionInfo());
        if (phantomReference != null) {
            phantomReference.clear();
        }
    }
View Full Code Here

     * @param obj the object on which to wait
     */
    public WaitUnreachable(Object obj) {
        this.obj = obj;
        queue = new ReferenceQueue();
        ref = new PhantomReference(obj, queue);
    }
View Full Code Here

    ReferenceQueue q = new ReferenceQueue();

    Reference ar = new WeakReference(a);
    Reference br = new WeakReference(b, q);
    Reference cr = new WeakReference(c, q);
    Reference dr = new PhantomReference(d, q);
    Reference er = new MyReference(e, q, "foo");
   
    WeakHashMap<Key,Object> map = new WeakHashMap();
    map.put(new Key("foo"), f);

    a = b = c = d = e = cr = null;
   
    System.out.println("a: " + ar.get());
    System.out.println("b: " + br.get());
    System.out.println("d: " + dr.get());
    System.out.println("e: " + er.get());
    System.out.println("f: " + map.get(new Key("foo")));

    System.gc();

    System.out.println("a: " + ar.get());
    System.out.println("b: " + br.get());
    System.out.println("d: " + dr.get());
    System.out.println("e: " + er.get());
    System.out.println("f: " + map.get(new Key("foo")));

    for (Reference r = q.poll(); r != null; r = q.poll()) {
      System.out.println("polled: " + r.get());     
View Full Code Here

        if (target instanceof DisposerTarget) {
            target = ((DisposerTarget)target).getDisposerReferent();
        }
        java.lang.ref.Reference ref;
        if (refType == PHANTOM) {
            ref = new PhantomReference(target, queue);
        } else {
            ref = new WeakReference(target, queue);
        }
        records.put(ref, rec);
    }
View Full Code Here

TOP

Related Classes of java.lang.ref.PhantomReference

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.