Java 8 ReferenceQueue - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 ReferenceQueue class.

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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
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
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
		} 
	} 
 
	private final int _maxIdleSize;
	private final PoolAction<V, P> _poolAction;
	private final ReferenceQueue<V> _referenceQueue = new ReferenceQueue<V>();
	private final Queue<SoftReference<? extends V>> _softReferences =
		new ConcurrentLinkedQueue<SoftReference<? extends V>>();
	private final boolean _useWeakCounter;
	private final AtomicInteger _weakCounter;
 
} 
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
    public static <V> Closure<V> buildSoftReferenceMemoizeFunction(final int protectedCacheSize, final MemoizeCache<Object, Object> cache, final Closure<V> closure) {
        final ProtectionStorage lruProtectionStorage = protectedCacheSize > 0 ?
                new LRUProtectionStorage(protectedCacheSize) :
                new NullProtectionStorage(); // Nothing should be done when no elements need protection against eviction 
 
        final ReferenceQueue queue = new ReferenceQueue();
 
        return new SoftReferenceMemoizeFunction<V>(cache, closure, lruProtectionStorage, queue);
    } 
 
    /** 
     * Creates a key to use in the memoize cache 
     * 
     * @param args The arguments supplied to the closure invocation 
     * 
     * @return The key - a list holding all arguments 
     */ 
    private static Object generateKey(final Object[] args) { 
        if (args == null) return Collections.emptyList();
        Object[] copyOfArgs = copyOf(args, args.length);
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
/** 
 * Implements a weak value reference HashMap that removes entries from the map once the value has been garbage collected.<br> <br> The queue is polled whenever an entry is added or removed from the 
 * map 
 */ 
public class WeakValueHashMap<K, V> { 
	private final ReferenceQueue<V> referenceQueue = new ReferenceQueue<>();
	protected final HashMap<K, KeyReference> map = new HashMap<>();
 
	/** 
	 * Puts the given key, value pair into the map.  Any expired keys are automatically flushed. 
	 * 
	 * @return the old value, or null if the key didn't map to a value 
	 */ 
	public V put(K key, V value) {
		flushKeys(); 
		Reference<V> ref = map.put(key, new KeyReference(key, value, referenceQueue));
		if (ref != null) {
			return ref.get();
		} else { 
			return null; 
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
 */ 
public class WeakHashSet<T> extends AbstractSet<T> {
 
	Map<WO, WO> elementSet = new ConcurrentHashMap<WO, WO>();
 
	ReferenceQueue<T> referenceQueue = new ReferenceQueue<T>();
 
	private static Object NULL = new Object(); 
 
	protected int hashCode(T val) {
		return val.hashCode();
	} 
	 
	protected boolean equals(T v1, T v2) {
		return v1.equals(v2);
	} 
	 
	class WO extends WeakReference<T> {
 
		private int hashCode;
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods: poll, remove
 
    /** 
     * java.lang.ref.ReferenceQueue#ReferenceQueue() 
     */ 
    public void test_Constructor() { 
        ReferenceQueue rq = new ReferenceQueue();
        assertNull(rq.poll());
        try { 
            rq.remove(100L);
        } catch (InterruptedException e) {
            fail("InterruptedException was thrown.");
        } 
    } 
 
    protected void setUp() { 
        rq = new ReferenceQueue();
    } 
 
    protected void tearDown() { 
    } 
Full Snippet Info
6
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
See Code Examples for Java 8 ReferenceQueue Methods: