当前位置: 首页>>代码示例>>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;未经允许,请勿转载。