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