java.lang.ref.PhantomReference#isEnqueued ( )源码实例Demo

下面列出了java.lang.ref.PhantomReference#isEnqueued ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: JavaRushTasks   文件: Solution.java
public void checkListWithReferences(List<PhantomReference<Monkey>> list, String string) {
    int count = 0;
    for (PhantomReference<Monkey> reference : list) {
        if (reference.isEnqueued()) {
            count++;
        }
    }

    System.out.println(String.format("The enqueue reference count is %d (%s GC was called)", count, string));
}
 
源代码2 项目: cglib   文件: TestEnhancer.java
/**
 * Verifies that the cache in {@link AbstractClassGenerator} SOURCE doesn't
 * leak class definitions of classloaders that are no longer used.
 */
public void testSourceCleanAfterClassLoaderDispose() throws Throwable {
    ClassLoader custom = new ClassLoader(this.getClass().getClassLoader()) {

        @Override
        public Class<?> loadClass(String name) throws ClassNotFoundException {
            if (EA.class.getName().equals(name)) {
                InputStream classStream = this.getClass().getResourceAsStream("/net/sf/cglib/proxy/EA.class");
                byte[] classBytes;
                try {
                    classBytes = toByteArray(classStream);
                    return this.defineClass(null, classBytes, 0, classBytes.length);
                } catch (IOException e) {
                    return super.loadClass(name);
                }
            } else {
                return super.loadClass(name);
            }
        }
    };

    PhantomReference<ClassLoader> clRef = new PhantomReference<ClassLoader>(custom,
            new ReferenceQueue<ClassLoader>());

    buildAdvised(custom);
    custom = null;

    for (int i = 0; i < 10; ++i) {
        System.gc();
        Thread.sleep(100);
        if (clRef.isEnqueued()) {
            break;
        }
    }
    assertTrue("CGLIB should allow classloaders to be evicted. PhantomReference<ClassLoader> was not cleared after 10 gc cycles," +
            "thus it is likely some cache is preventing the class loader to be garbage collected", clRef.isEnqueued());

}
 
 同类方法