Example usage for java.lang.ref ReferenceQueue ReferenceQueue

List of usage examples for java.lang.ref ReferenceQueue ReferenceQueue

Introduction

In this page you can find the example usage for java.lang.ref ReferenceQueue ReferenceQueue.

Prototype

public ReferenceQueue() 

Source Link

Document

Constructs a new reference-object queue.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReferenceQueue rq = new ReferenceQueue();
    WeakReference<String> wr = new WeakReference<String>("string", rq);

    while (true) {
        Reference r = rq.remove();
        if (r == wr) {
            System.out.println("no longer referenced");
        }/*from  w  w w.ja  va2  s  .  c  o m*/
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReferenceQueue rq = new ReferenceQueue();
    PhantomReference<String> pr = new PhantomReference<String>("object", rq);

    while (true) {
        Reference r = rq.remove();
        if (r == pr) {
            // about to be reclaimed.
            r.clear();//  ww w  .jav a  2s .c  o  m
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*from  w  ww.j  a va 2s.co  m*/
    };
    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }//w w w.  j  a  v a 2s . c  om
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing PhantomReference.");
    reference = new PhantomReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*  w w w.  j  av a  2 s  .  c o  m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*from  ww  w . ja  va  2 s .co m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing WeakReference.");
    reference = new WeakReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:xiaofei.library.hermes.util.HermesCallbackGc.java

private HermesCallbackGc() {
    mReferenceQueue = new ReferenceQueue<Object>();
    mTimeStamps = new ConcurrentHashMap<PhantomReference<Object>, Triple<IHermesServiceCallback, Long, Integer>>();
}

From source file:org.polymap.core.runtime.cache.Soft2CacheManager.java

protected Soft2CacheManager() {
    caches = new MapMaker().initialCapacity(256).weakValues().makeMap();
    refQueue = new ReferenceQueue();

    // start thread
    thread = new QueueChecker();
    thread.start();//from ww  w.j a va  2  s  .  c  om
}

From source file:org.entrystore.repository.impl.SoftCache.java

public SoftCache() {
    clearedRefs = new ReferenceQueue<Entry>();

    // start thread to delete cleared references from the cache
    remover = new Remover(clearedRefs, this);
    remover.start();/*  w w w .  j av a  2 s.  c  o  m*/

    // add a shutdown hook to interrupt the endless loop
    // the hook is only called when the whole VM is shutdown
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            shutdown();
        }
    });
}

From source file:WeakCanonicalSet.java

public WeakCanonicalSet() {
    final int initialCapacity = 101;
    final float loadFactor = 0.75f;
    this.loadFactor = loadFactor;
    this.table = new Entry[initialCapacity];
    this.threshold = (int) (initialCapacity * loadFactor);
    this.queue = new ReferenceQueue<T>();
}