java Reference 中的 next 成员变量的作用是什么

发布于 2022-09-04 04:49:03 字数 476 浏览 13 评论 0

在 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 为空,那么具体会做什么处理,非空则会正常对待,那么所谓的正常对待是指什么。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

决绝 2022-09-11 04:49:03
/* 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回收机制的人才能解答了。
等待大神吧。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文