定义:未最终化对象与可最终化对象

发布于 2024-11-15 20:28:30 字数 468 浏览 3 评论 0原文

为了理解 Java 中的弱引用,我不得不查阅 Java 语言规范。第 12.6 节中的以下部分让我感到困惑:

未终结的对象从未自动调用其终结器; 已完成的对象已自动调用其终结器。一个可最终确定的 对象从未自动调用其终结器,但 Java 虚拟对象 机器最终可能会自动调用其终结器。

那么未最终化对象和可最终化对象之间的形式区别是什么?从引用看来,如果未最终化和可最终化是不同的,那么对于未最终化的对象来说,JVM 最终可能会调用其终结器,这一定是。有点令人困惑,或者我还有一些英语语义需要学习;)

链接到 Java 规范中的部分:实现终结

In order to understand weak references in Java, I have had to consult the Java Language Specification. The following part, from section 12.6, puzzles me:

An unfinalized object has never had its finalizer automatically invoked;
a finalized object has had its finalizer automatically invoked. A finalizable
object has never had its finalizer automatically invoked, but the Java virtual
machine may eventually automatically invoke its finalizer.

So what is the formal difference between an unfinalized and a finalizable object ? From the quote it seems that if unfinalized and finalizable are to be different, then for an unfinalized object it must be the case that it is not true that the JVM may eventually invoke its finalizer. A little confusing or I still have some English semantics to study ;)

Link to the section in the Java spec: Implementing Finalization

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

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

发布评论

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

评论(3

同展鸳鸯锦 2024-11-22 20:28:30

答案似乎就在这一行:

如果Java虚拟机检测到一个未终结的对象已经变为finalizer-reachable或unreachable,它可能会将该对象标记为finalized(G,H);

未最终确定的对象尚不符合最终确定的条件。他们是可以到达的。可终结的对象有资格被终结,因此 JVM 可以在选择时执行此操作。换句话说,“可能”的含义是“有权”,而不仅仅是“可能发生”的含义。

The answer seems to lie in this line:

If the Java virtual machine detects that an unfinalized object has become finalizer-reachable or unreachable, it may label the object finalizable (G, H);

Unfinalized objects are not eligible for finalization yet. They are reachable. Finalizable objects are eligible to be finalized, so the JVM may do that when it chooses. In other words, "may" in the sense of "has permission to" not just in the sense of "it might happen."

抠脚大汉 2024-11-22 20:28:30

未终结对象和可终结对象之间的区别在于,第二个对象上的终结器可以在将来的任何时间自动调用,而未终结对象上的终结器不能除非该对象首先变为可终结的,否则将自动调用。

  • 在此状态下,未终结的对象不会由 JVM 自动调用其终结器
  • 可终结的对象最终会由 JVM 自动调用其终结器

The difference between an unfinalized and a finalizable object is that the finalizer on the second one could be automatically invoked at any time in the future, while the finalizer on the unfinalized object can't be automatically invoked, unless the object first becomes finalizable.

  • a unfinalized object will not get its finalizer automatically invoked by the JVM in this state
  • a finalizable object can eventually get its finalizer automatically invoked by the JVM
你怎么这么可爱啊 2024-11-22 20:28:30

不保证 GC 会被执行,也不保证 finalize() 会被调用。它很可能会在某个时候发生。

当一个对象不再有强引用时,它可以被垃圾收集。一段时间后,可以执行 GC,并将对象添加到终结队列中,以调用其 finalize() 方法。一旦该方法被调用,如果仍然没有对它的强引用,则可以将其删除。

There is no guarantee that a GC will ever be performed or that finalize() will ever be called. It is highly likely that it will happen at some point.

When an object no longer has a strong reference to it, it can be garbage collected. Some time later a GC can be performed and the object is added to a finalization queue to have its finalize() method called. Once the method has been called it can be removed if there still is not strong reference to it.

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