为什么自Java 9 PhantomReference java doc声明它专用于POST尸体清理操作,尽管之前它是PRE尸体 [英] Why since java 9 PhantomReference java doc states that it is dedicated to the POST-mortem cleanup actions although it was PRE-mortem before

查看:97
本文介绍了为什么自Java 9 PhantomReference java doc声明它专用于POST尸体清理操作,尽管之前它是PRE尸体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PhantomReference 用于Java的Java文档8 和更少的样子:

虚拟参考对象,在收集器之后排入队列 确定可以以其他方式收回其推荐对象.幻影 引用最常用于安排事前清理 操作,比Java更加灵活 最终确定机制.如果垃圾收集器确定在 幻影参照物的参照物在某个时间点是 幻影可以到达,那么在那时或以后的某个时间 使参考排队.

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.

为了确保保留可回收对象,引用对象 的幻像引用可能无法检索: 幻像引用始终返回null.

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的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.

为了确保保留可回收对象,引用对象 的幻像引用可能无法检索: 幻像引用始终返回null.

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创始人重新考虑了对此类的奉献?

Was something changing in PhantomReference behaviour in java 9? or just java founders rethought dedication of that class ?

推荐答案

自Java 9起,PhantomReference(PR)为

Since Java 9, PhantomReference (PR) are automatically cleared. What you see is the Javadoc change that comes as the result of that change.

在Java 9之前,PR引用的对象保持活动状态,即使其get()将返回null.因此,在PR本身失效之前,尽管您无法获得对PR的引用,但该引用在技术上仍将处于活动状态.这种行为的好处还不是很清楚.无论如何,公关处理将是事前清理".

Before Java 9, the object referenced by PR was kept alive, even though its get() would return null. Therefore, until PR itself is dead, the referent would be technically alive, although you could not acquire the reference to it. The benefits of this behavior are not very clear. Anyhow, PR handling would be the "pre-mortem cleanup".

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

After Java 9, PR is cleared right before enqueueing (just like other types of weak/soft refs), the referent itself becomes fully dead before PR is processed by application code, which would be the "post-mortem cleanup".

这篇关于为什么自Java 9 PhantomReference java doc声明它专用于POST尸体清理操作,尽管之前它是PRE尸体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆