gpt4 book ai didi

java - 为什么从 java 9 PhantomReference java doc 开始,它专门用于 POST-mortem 清理操作,尽管它之前是 PRE-mortem

转载 作者:行者123 更新时间:2023-12-01 19:37:53 26 4
gpt4 key购买 nike

PhantomReference java doc for java 8更少看起来像这样:

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. If the garbage collector determines at a certain point in time that the referent of a phantom reference is phantom reachable, then at that time or at some later time it will enqueue the reference.

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

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

PhantomReference java doc for java 9更高的看起来像这样:

Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used to schedule post-mortem cleanup actions. Suppose the garbage collector determines at a certain point in time that an object is phantom reachable. At that time it will atomically clear all phantom references to that object and all phantom references to any other phantom-reachable objects from which that object is reachable. At the same time or at some later time it will enqueue those newly-cleared phantom references that are registered with reference queues.

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

java 9 中的 PhantomReference 行为是否发生了变化?或者只是 Java 创始人重新考虑了该类的奉献?

最佳答案

自 Java 9 起,PhantomReference (PR) 为 automatically cleared 。您看到的是由于该更改而产生的 Javadoc 更改。

在 Java 9 之前,PR 引用的对象保持 Activity 状态,即使其 get() 将返回 null。因此,在 PR 本身死亡之前,所指对象在技术上仍然是活着的,尽管您无法获取对它的引用。这种行为的好处还不是很清楚。无论如何,公关处理将是“事前清理”。

在 Java 9 之后,PR 在入队之前被清除(就像其他类型的弱/软引用一样),在应用程序代码处理 PR 之前,引用对象本身就完全死亡,这将是“事后清理”。

关于java - 为什么从 java 9 PhantomReference java doc 开始,它专门用于 POST-mortem 清理操作,尽管它之前是 PRE-mortem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56684150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com