源码 - java Reference 中的 next 成员变量的作用是什么
怪我咯
怪我咯 2017-04-18 10:22:42
0
1
347

在 JDK 的 java.lang.ref 包中的 Reference 有四种状态,分别是 Active,Pending,Enqueued,Inactive。它的一个好处根据源码就是:

* With this scheme the collector need only examine the next field in order
* to determine whether a Reference instance requires special treatment: If
* the next field is null then the instance is active; if it is non-null,
* then the collector should treat the instance normally.

那么,特殊对待是指什么,如果 next field 为空,那么具体会做什么处理,非空则会正常对待,那么所谓的正常对待是指什么。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(1)
小葫芦
/* A Reference instance is in one of four possible internal states:
 *
 *     Active: Subject to special treatment by the garbage collector. Some
 *     time after the collector detects that the reachability of the
 *     referent has changed to the appropriate state, it changes the
 *     instance's state to either Pending or Inactive, depending upon
 *     whether or not the instance was registered with a queue when it was
 *     created.  In the former case it also adds the instance to the
 *     pending-Reference list. Newly-created instances are Active.
 *
 *     Pending: An element of the pending-Reference list, waiting to be
 *     enqueued by the Reference-handler thread. Unregistered instances
 *     are never in this state.
 *
 *     Enqueued: An element of the queue with which the instance was
 *     registered when it was created.  When an instance is removed from
 *     its ReferenceQueue, it is made Inactive. Unregistered instances are
 *     never in this state.
 *
 *     Inactive: Nothing more to do.  Once an instance becomes Inactive its
 *     state will never change again.
 *
 * The state is encoded in the queue and next fields as follows:
 *
 *     Active: queue = ReferenceQueue with which instance is registered, or
 *     ReferenceQueue.NULL if it was not registered with a queue; next =
 *     null.
 *
 *     Pending: queue = ReferenceQueue with which instance is registered;
 *     next = this
 *
 *     Enqueued: queue = ReferenceQueue.ENQUEUED; next = Following instance
 *     in queue, or this if at end of list.
 *
 *     Inactive: queue = ReferenceQueue.NULL; next = this.
 *
 * With this scheme the collector need only examine the next field in order
 * to determine whether a Reference instance requires special treatment: If
 * the next field is null then the instance is active; if it is non-null,
 * then the collector should treat the instance normally.
 *
 * To ensure that a concurrent collector can discover active Reference
 * objects without interfering with application threads that may apply
 * the enqueue() method to those objects, collectors should link
 * discovered objects through the discovered field. The discovered
 * field is also used for linking Reference objects in the pending list.
 */

我把比较完整的注释贴上来了,这里可以知道流程的转换过程。
但是具体的工作细节,这个估计只有熟悉具体GC回收机制的人才能解答了。
等待大神吧。

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!