Search in sources :

Example 71 with Reference

use of java.lang.ref.Reference in project openj9 by eclipse.

the class Test_ReferenceQueue method test_remove2.

/**
 * @tests java.lang.ref.ReferenceQueue#remove(long)
 */
@Test
public void test_remove2() {
    // no matter what x is.
    try {
        AssertJUnit.assertTrue("Queue is empty.", rq.poll() == null);
        AssertJUnit.assertTrue("Queue is empty.", rq.remove((long) 1) == null);
        Thread ct = Support_ExtendedTestEnvironment.getInstance().getThread(new ChildThread());
        ct.start();
        // Thread.currentThread().sleep(100);
        Reference ret = rq.remove(0L);
        AssertJUnit.assertTrue("Delayed remove failed.", ret != null);
    // assertTrue("Delayed remove failed.", ((Integer)rq.remove(1L).get()).intValue()==667);
    } catch (InterruptedException e) {
        AssertJUnit.assertTrue("InterruptedExeException during test.", false);
    } catch (Exception e) {
        e.printStackTrace();
        AssertJUnit.assertTrue("Exception during test" + e.toString(), false);
    }
}
Also used : Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) Test(org.testng.annotations.Test)

Example 72 with Reference

use of java.lang.ref.Reference in project KeePassDX by Kunzisoft.

the class AbstractReferenceMap method purge.

/**
 * Purges stale mappings from this map.
 * <p>
 * Note that this method is not synchronized!  Special
 * care must be taken if, for instance, you want stale
 * mappings to be removed on a periodic basis by some
 * background thread.
 */
protected void purge() {
    Reference ref = queue.poll();
    while (ref != null) {
        purge(ref);
        ref = queue.poll();
    }
}
Also used : Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Example 73 with Reference

use of java.lang.ref.Reference in project egit by eclipse.

the class RepositoryCache method prune.

private void prune() {
    List<File> toRemove = new ArrayList<>();
    synchronized (repositoryCache) {
        for (Iterator<Map.Entry<File, Reference<Repository>>> i = repositoryCache.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<File, Reference<Repository>> entry = i.next();
            Repository repository = entry.getValue().get();
            if (repository == null || !repository.getDirectory().exists()) {
                i.remove();
                toRemove.add(entry.getKey());
            }
        }
    }
    IndexDiffCache cache = Activator.getDefault().getIndexDiffCache();
    if (cache != null) {
        for (File f : toRemove) {
            cache.remove(f);
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) ArrayList(java.util.ArrayList) IndexDiffCache(org.eclipse.egit.core.internal.indexdiff.IndexDiffCache) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 74 with Reference

use of java.lang.ref.Reference in project spring-loaded by spring-projects.

the class JVMPlugin method clearThreadGroupContext.

private boolean clearThreadGroupContext(Class<?> clazz) {
    boolean beanInfoCacheCleared = false;
    try {
        if (threadGroupContextClass == null) {
            threadGroupContextClass = Class.forName("java.beans.ThreadGroupContext", true, Introspector.class.getClassLoader());
        }
        if (threadGroupContextClass != null) {
            if (threadGroupContext_contextsField == null) {
                threadGroupContext_contextsField = threadGroupContextClass.getDeclaredField("contexts");
                threadGroupContext_removeBeanInfoMethod = threadGroupContextClass.getDeclaredMethod("removeBeanInfo", Class.class);
            }
            if (threadGroupContext_contextsField != null) {
                threadGroupContext_contextsField.setAccessible(true);
                Object threadGroupContext_contextsField_value = threadGroupContext_contextsField.get(null);
                if (threadGroupContext_contextsField_value == null) {
                    beanInfoCacheCleared = true;
                } else {
                    if (threadGroupContext_contextsField_value instanceof Map) {
                        // Indicates Java 7 up to rev21
                        Map<?, ?> m = (Map<?, ?>) threadGroupContext_contextsField_value;
                        Collection<?> threadGroupContexts = m.values();
                        for (Object o : threadGroupContexts) {
                            threadGroupContext_removeBeanInfoMethod.setAccessible(true);
                            threadGroupContext_removeBeanInfoMethod.invoke(o, clazz);
                        }
                        beanInfoCacheCleared = true;
                    } else {
                        // At update Java7u21 it changes
                        Class weakIdentityMapClazz = threadGroupContext_contextsField.getType();
                        Field tableField = weakIdentityMapClazz.getDeclaredField("table");
                        tableField.setAccessible(true);
                        Reference<?>[] refs = (Reference[]) tableField.get(threadGroupContext_contextsField_value);
                        Field valueField = null;
                        if (refs != null) {
                            for (int i = 0; i < refs.length; i++) {
                                Reference<?> r = refs[i];
                                Object o = (r == null ? null : r.get());
                                if (o != null) {
                                    if (valueField == null) {
                                        valueField = r.getClass().getDeclaredField("value");
                                    }
                                    valueField.setAccessible(true);
                                    Object threadGroupContext = valueField.get(r);
                                    threadGroupContext_removeBeanInfoMethod.setAccessible(true);
                                    threadGroupContext_removeBeanInfoMethod.invoke(threadGroupContext, clazz);
                                }
                            }
                        }
                        beanInfoCacheCleared = true;
                    }
                }
            }
        }
    } catch (Throwable t) {
        System.err.println("Unexpected problem clearing ThreadGroupContext beaninfo: ");
        t.printStackTrace();
    }
    return beanInfoCacheCleared;
}
Also used : Field(java.lang.reflect.Field) Reference(java.lang.ref.Reference) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Aggregations

Reference (java.lang.ref.Reference)74 WeakReference (java.lang.ref.WeakReference)34 SoftReference (java.lang.ref.SoftReference)33 ReferenceQueue (java.lang.ref.ReferenceQueue)13 Field (java.lang.reflect.Field)13 Map (java.util.Map)13 HashMap (java.util.HashMap)11 PhantomReference (java.lang.ref.PhantomReference)10 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Tracing (brave.Tracing)3 Platform (brave.internal.Platform)3 TraceContext (brave.propagation.TraceContext)3 BufferedImage (java.awt.image.BufferedImage)3 VolatileImage (java.awt.image.VolatileImage)3 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 Iterator (java.util.Iterator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3