Best code examples for Java 8 ReferenceQueue class (java.lang.ref.ReferenceQueue)

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 ReferenceQueue class.
You can now enable Codota on your own code to easily search and navigate your Java codebase.

Java 8 ReferenceQueue examples from Open Source projects
This code example shows how to use the following methods:
public class LRUCache<K, V> { 
 
	final private int capacity;
	final private Map<K, Reference<V>> map;
	final private ReentrantLock lock = new ReentrantLock();
	final private ReferenceQueue<V> queue = new ReferenceQueue<V>();
 
	public LRUCache(int capacity) {
		this.capacity = capacity;
		map = new LinkedHashMap<K, Reference<V>>(capacity, 1f, true) {
			@Override 
			protected boolean removeEldestEntry( 
					Map.Entry<K, Reference<V>> eldest) {
				return this.size() > LRUCache.this.capacity;
			} 
		}; 
	} 
 
	public V put(K key, V value) {
		lock.lock();
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods:
public class WeakReferenceMonitor {
 
	private static final Log logger = LogFactory.getLog(WeakReferenceMonitor.class);
 
	// Queue receiving reachability events 
	private static final ReferenceQueue<Object> handleQueue = new ReferenceQueue<Object>();
 
	// All tracked entries (WeakReference => ReleaseListener) 
	private static final Map<Reference<?>, ReleaseListener> trackedEntries = new HashMap<Reference<?>, ReleaseListener>();
 
	// Thread polling handleQueue, lazy initialized 
	private static Thread monitoringThread = null;
 
 
	/** 
	 * 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 
	 */ 
Full Snippet Info
7
CodeRank
This code example shows how to use the following methods: remove
                        cl.getConstructor(OutputStream.class);
 
                OutputStream os = new ByteArrayOutputStream();
                ObjectOutputStream obj = cons.newInstance(os);
 
                final ReferenceQueue<Class<?>> queue = new ReferenceQueue<Class<?>>();
                WeakReference<Class<?>> ref = new WeakReference<Class<?>>(cl, queue);
 
                cl = null;
                obj = null;
                loader = null;
                cons = null;
                systemLoader = null;
 
                System.err.println("\nStart Garbage Collection right now");
                System.gc();
 
                Reference<? extends Class<?>> dequeued = queue.remove(TIMEOUT);
                if (dequeued == ref) {
                        System.err.println("\nTEST PASSED");

6
CodeRank
This code example shows how to use the following methods: sleep, remove
 
            MarshalledObject mobj = new MarshalledObject(stub);
            stub = (Remote) mobj.get();
            System.err.println("marshalled/unmarshalled stub: " + stub);
 
            ReferenceQueue refQueue = new ReferenceQueue();
            Reference weakRef = new WeakReference(impl, refQueue);
            impl = null;
            System.gc();
            if (refQueue.remove(TIMEOUT) == weakRef) {
                throw new RuntimeException(
                    "TEST FAILED: remote object garbage collected"); 
            } else { 
                System.err.println("TEST PASSED");
                stub = null;
                System.gc();
                Thread.sleep(2000);
                System.gc();
            } 
        } finally { 

7
CodeRank
This code example shows how to use the following methods: remove
  private WeakKeySetUtils() {} 
 
  public static void awaitFullGc() { 
    // GcFinalization *should* do it, but doesn't work well in practice... 
    // so we put a second latch and wait for a ReferenceQueue to tell us. 
    ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
    WeakReference ref = new WeakReference<Object>(new Object(), queue);
    GcFinalization.awaitFullGc(); 
    try { 
      assertSame("queue didn't return ref in time", ref, queue.remove(5000));
    } catch (IllegalArgumentException e) {
      throw new RuntimeException(e);
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    } 
  } 
 
  public static void awaitClear(WeakReference<?> ref) {
    // GcFinalization *should* do it, but doesn't work well in practice... 
    // so we put a second latch and wait for a ReferenceQueue to tell us. 
Full Snippet Info
6
CodeRank
See Code Examples for Java 8 ReferenceQueue Methods: