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


Java PhantomReference.get方法代碼示例

本文整理匯總了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());
}
 
開發者ID:vitaly-chibrikov,項目名稱:otus_java_2017_06,代碼行數:22,代碼來源:Main.java

示例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());

}
 
開發者ID:vitaly-chibrikov,項目名稱:otus_java_2017_04,代碼行數:23,代碼來源:Main.java

示例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");
}
 
開發者ID:shannah,項目名稱:cn1,代碼行數:30,代碼來源:PhantomReferenceTest.java


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