List of usage examples for java.lang.ref WeakReference WeakReference
public WeakReference(T referent, ReferenceQueue<? super T> q)
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 ww w .j a v a 2s. c om*/ } }
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 ww . ja v a 2 s. c om*/ }; 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:WeakBag.java
/** * Add o to the bag and return the WeakReference that can be used to * remove it.//from w w w .j a va 2 s .c om */ public WeakReference add(Object o) { WeakReference ans = new WeakReference(o, refQueue); set.add(ans); return ans; }
From source file:WeakValueMap.java
protected Reference newReference(Object value) { return new WeakReference(value, refQueue); }
From source file:com.mawujun.util.other.WeakReferenceMonitor.java
/** * Start to monitor given handle object for becoming weakly reachable. * When the handle isn't used anymore, the given listener will be called. * @param handle the object that will be monitored * @param listener the listener that will be called upon release of the handle *///from w w w . j a va 2s . co m public static void monitor(Object handle, ReleaseListener listener) { if (logger.isDebugEnabled()) { logger.debug("Monitoring handle [" + handle + "] with release listener [" + listener + "]"); } // Make weak reference to this handle, so we can say when // handle is not used any more by polling on handleQueue. WeakReference weakRef = new WeakReference(handle, handleQueue); // Add monitored entry to internal map of all monitored entries. addEntry(weakRef, listener); }
From source file:com.dianping.resource.io.util.WeakReferenceMonitor.java
/** * Start to monitor given handle object for becoming weakly reachable. * When the handle isn't used anymore, the given listener will be called. * @param handle the object that will be monitored * @param listener the listener that will be called upon release of the handle *//* w w w .j a v a 2s. co m*/ public static void monitor(Object handle, ReleaseListener listener) { if (logger.isDebugEnabled()) { logger.debug("Monitoring handle [" + handle + "] with release listener [" + listener + "]"); } // Make weak reference to this handle, so we can say when // handle is not used any more by polling on handleQueue. WeakReference<Object> weakRef = new WeakReference<Object>(handle, handleQueue); // Add monitored entry to internal map of all monitored entries. addEntry(weakRef, listener); }