本文整理匯總了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");
}
}
示例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();
}
}
示例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);
}
示例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();
}
}