PhantomReference源码解析

1.简介

Phantom reference objects, which are enqueued after the collector
determines that their referents may otherwise be reclaimed.  Phantom
references are most often used for scheduling pre-mortem cleanup actions in
a more flexible way than is possible with the Java finalization mechanism.

在引用对象被回收后,虚引用会入队。虚引用最常用于调度预清理操作,比Java的finalization机制更灵活。

<p> If the garbage collector determines at a certain point in time that the
referent of a phantom reference is <a
href="package-summary.html#reachability">phantom reachable</a>, then at that
time or at some later time it will enqueue the reference.

但引用对象变为虚可达后,则虚引用会入队。

<p> In order to ensure that a reclaimable object remains so, the referent of
a phantom reference may not be retrieved: The <code>get</code> method of a
phantom reference always returns <code>null</code>.

get方法总是返回null

<p> Unlike soft and weak references, phantom references are not
automatically cleared by the garbage collector as they are enqueued.  An
object that is reachable via phantom references will remain so until all
such references are cleared or themselves become unreachable.

不同于软引用和弱引用,因为虚引用会入队所以gc不会自动清除虚引用。

PhantomReference,即虚引用,虚引用并不会影响对象的生命周期。虚引用的作用为:跟踪垃圾回收器收集对象这一活动的情况。

2.构造器

public class PhantomReference<T> extends Reference<T> {

    /**
     * Creates a new phantom reference that refers to the given object and
     * is registered with the given queue.
     *
     * <p> It is possible to create a phantom reference with a <tt>null</tt>
     * queue, but such a reference is completely useless: Its <tt>get</tt>
     * method will always return null and, since it does not have a queue, it
     * will never be enqueued.
     *
     * @param referent the object the new phantom reference will refer to
     * @param q the queue with which the reference is to be registered,
     *          or <tt>null</tt> if registration is not required
     */
    public PhantomReference(T referent, ReferenceQueue<? super T> q) {
        super(referent, q);
    }

如果ReferenceQueue为空,那么虚引用将毫无作用,因此PhantomReference必须要和ReferenceQueue联合使用。而SoftReference和WeakReference可以选择和ReferenceQueue联合使用也可以不选择,这使他们的区别之一。

3.get

public T get() {
        return null;
    }

4.实例

public class TestPhantomReference {

    private static ReferenceQueue<Object> rq = new ReferenceQueue<Object>();

    public static void main(String[] args) {

        Object obj = new Object();
        PhantomReference<Object> pr = new PhantomReference<Object>(obj, rq);
        System.out.println(pr.get());
        Reference<Object> r = (Reference<Object>) rq.poll();
        if (r == null) {
            System.out.println("before gc ReferenceQueue is null, pr is " + pr);
        }
        obj = null;
        System.gc();
        System.out.println(pr.get());
        r = (Reference<Object>) rq.poll();
        if (r != null) {
            System.out.println("after gc, pr enqueue, pr is " + pr);
        }
    }
}

结果如下:

null
before gc ReferenceQueue is null, pr is java.lang.ref.PhantomReference@6d6f6e28
null
after gc, pr enqueue, pr is java.lang.ref.PhantomReference@6d6f6e28
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容