當前位置: 首頁>>代碼示例>>Java>>正文


Java ReferenceQueue.remove方法代碼示例

本文整理匯總了Java中java.lang.ref.ReferenceQueue.remove方法的典型用法代碼示例。如果您正苦於以下問題:Java ReferenceQueue.remove方法的具體用法?Java ReferenceQueue.remove怎麽用?Java ReferenceQueue.remove使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.ref.ReferenceQueue的用法示例。


在下文中一共展示了ReferenceQueue.remove方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testCleanerTermination

import java.lang.ref.ReferenceQueue; //導入方法依賴的package包/類
/**
 * Test that releasing the reference to the Cleaner service allows it to be
 * be freed.
 */
@Test
void testCleanerTermination() {
    ReferenceQueue<Object> queue = new ReferenceQueue<>();
    Cleaner service = Cleaner.create();

    PhantomReference<Object> ref = new PhantomReference<>(service, queue);
    System.gc();
    // Clear the Reference to the cleaning service and force a gc.
    service = null;
    System.gc();
    try {
        Reference<?> r = queue.remove(1000L);
        Assert.assertNotNull(r, "queue.remove timeout,");
        Assert.assertEquals(r, ref, "Wrong Reference dequeued");
    } catch (InterruptedException ie) {
        System.out.printf("queue.remove Interrupted%n");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:CleanerTest.java

示例2: main

import java.lang.ref.ReferenceQueue; //導入方法依賴的package包/類
public static final void main(String[] args) throws Exception {
        System.err.println("\n Regression test for bug 6232010\n");
        if (System.getSecurityManager() == null) {
                System.setSecurityManager(new SecurityManager());
        }

        ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
        ClassLoader loader = new URLClassLoader(((URLClassLoader) systemLoader).getURLs(),
                                                                                systemLoader.getParent());
        Class<? extends ObjectOutputStream> cl =
                Class.forName(SubclassOfOOS.class.getName(), false,
                                          loader).asSubclass(ObjectOutputStream.class);

        Constructor<? extends ObjectOutputStream> cons =
                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");
        } else {
                throw new Error();
        }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:39,代碼來源:SubclassGC.java

示例3: removeAll

import java.lang.ref.ReferenceQueue; //導入方法依賴的package包/類
void removeAll(ReferenceQueue<?> q, int n) throws InterruptedException {
    for (int j = n; j--> 0; ) {
        if (q.poll() == null) {
            for (;;) {
                System.gc();
                if (q.remove(1000) != null)
                    break;
                System.out.printf(
                    "%d/%d unqueued references remaining%n", j, n);
            }
        }
    }
    check(q.poll() == null);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:GCRetention.java

示例4: main

import java.lang.ref.ReferenceQueue; //導入方法依賴的package包/類
public static final void main(String[] args) throws Exception {
        System.err.println("\n Regression test for bug 6232010\n");
        if (System.getSecurityManager() == null) {
                System.setSecurityManager(new SecurityManager());
        }

        ClassLoader systemLoader = ClassLoader.getSystemClassLoader();
        URL testClassesURL = new File(System.getProperty("test.classes")).toURI().toURL();
        ClassLoader loader = new URLClassLoader(new URL[] { testClassesURL } ,
                                                systemLoader.getParent());
        Class<? extends ObjectOutputStream> cl =
                Class.forName(SubclassOfOOS.class.getName(), false,
                                          loader).asSubclass(ObjectOutputStream.class);

        Constructor<? extends ObjectOutputStream> cons =
                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");
        } else {
                throw new Error();
        }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:40,代碼來源:SubclassGC.java


注:本文中的java.lang.ref.ReferenceQueue.remove方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。