You must know the type of java reference — a detailed explanation of phantom reference source code

definition

Phantom reference is a virtual reference that does not affect the life cycle of an object, nor can it get an object instance from a virtual reference.

explain

In fact, the introduction of the source code is not much. The main contents are finished in the previous introduction. The source code of the phantom reference class is as simple as the WeakReference class:

public class PhantomReference<T> extends Reference<T> {
    public T get() {
        return null;
    }

    /**
     * 这里传入的引用队列也可以为null,但是这样的引用没有任何意义,因为永远不会入队
     */
    public PhantomReference(T referent,ReferenceQueue<? super T> q) {
        super(referent,q);
    }
}

As you can see, the get method directly returns null. There is a two parameter construction method that passes in the referenced object and reference queue.

Well, let's finish this article first.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>