本文整理汇总了Java中java.lang.ref.PhantomReference.get方法的典型用法代码示例。如果您正苦于以下问题:Java PhantomReference.get方法的具体用法?Java PhantomReference.get怎么用?Java PhantomReference.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.ref.PhantomReference
的用法示例。
在下文中一共展示了PhantomReference.get方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: phantom
import java.lang.ref.PhantomReference; //导入方法依赖的package包/类
private static void phantom() throws InterruptedException {
//Strong Reference
BigObject a = new BigObject();
//Creating ReferenceQueue
ReferenceQueue<BigObject> refQueue = new ReferenceQueue<>();
//Creating Phantom Reference to A-type object to which 'a' is also pointing
PhantomReference<BigObject> phantomA = new PhantomReference<>(a, refQueue);
System.out.println("Ref in pool before GC: " + refQueue.poll());
a = null;
//Now, A-type object to which 'a' is pointing earlier is available for garbage collection.
//But, this object is kept in 'refQueue' before removing it from the memory.
a = phantomA.get(); //it always returns null
System.gc();
Thread.sleep(100);
System.out.println("Ref in pool after GC: " + refQueue.poll());
}
示例2: phantom
import java.lang.ref.PhantomReference; //导入方法依赖的package包/类
private static void phantom() throws InterruptedException {
//Strong Reference
BigObject a = new BigObject();
//Creating ReferenceQueue
ReferenceQueue<BigObject> refQueue = new ReferenceQueue<>();
//Creating Phantom Reference to A-type object to which 'a' is also pointing
PhantomReference<BigObject> phantomA = new PhantomReference<>(a, refQueue);
System.out.println("Big object length:" + refQueue.poll());
a = null;
//Now, A-type object to which 'a' is pointing earlier is available for garbage collection.
//But, this object is kept in 'refQueue' before removing it from the memory.
a = phantomA.get(); //it always returns null
System.gc();
Thread.sleep(100);
System.out.println("Big object length:" + refQueue.poll());
}
示例3: main
import java.lang.ref.PhantomReference; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
ReferenceQueue queue = new ReferenceQueue();
Object referent = new PhantomReferenceTest();
ref = new PhantomReference(referent, queue);
// drop strong reference
referent = null;
System.gc();
// run finalization to be sure that the reference is enqueued
System.runFinalization();
Reference enqueued;
enqueued = queue.poll();
if (enqueued == null) {
System.out.println("FAIL: reference was not enqueued");
return;
}
if (ref.get() != null) {
System.out.println("FAIL: reference was not cleared.");
return;
}
if (enqueued.get() != null) {
System.out.println("FAIL: reference was not cleared.");
return;
}
System.out.println("PASS");
}