简体   繁体   English

关于Java敲定方法的面试题

[英]Interview Question on Java finalize method

I have came across difficult (for me) Java interview question on finalize method.我遇到了困难的(对我来说)Java 关于最终确定方法的面试问题。 Suppose you have given finalize method as shown below:假设你已经给出了 finalize 方法,如下所示:

public void finalize()
{
     a.b = this;
}

Now the following object scenario is given.现在给出下面的 object 场景。

在此处输入图像描述

How would you solve this problem?你将如何解决这个问题? If A was not referring to B then this problem could be easier as GC will run, it will collect B and call finalize for B but here A is referring B so its difficult.如果 A 没有引用 B,那么这个问题可能会更容易,因为 GC 将运行,它将收集 B 并为 B 调用 finalize,但这里 A 引用 B,所以它很困难。 How finalize will work in this scenario?在这种情况下,finalize 将如何工作?

Any ideas?有任何想法吗? Thanks in advance提前致谢

Very interesting question.非常有趣的问题。 From the JDK1.6 Doc, I find these two sentences: 1. The finalize method may take any action, including making this object available again to other threads 2. The finalize method is never invoked more than once by a Java virtual machine for any given object.从 JDK1.6 Doc 中,我发现这两个句子: 1. finalize 方法可以采取任何行动,包括使这个 object 再次可用于其他线程 2. finalize 方法永远不会被 Java 虚拟机调用多次给定 object。

So in my opinion, for the first time, when B is collecting by GC, finalize method will be invoked if A is still available to some threads then B becomes available again, this time the GC will not collect B. But because finalize method will be invoked only once, so next time when the GC find B can't be accessed by any thread then GC will collect B.所以在我看来,第一次,当 B 被 GC 收集时,如果 A 仍然对某些线程可用,则将调用 finalize 方法,然后 B 再次可用,这一次 GC 不会收集 B。但是因为 finalize 方法会只被调用一次,所以下次当 GC 发现 B 不能被任何线程访问时,GC 将收集 B。

The easiest way to think of Java finalization is to consider it an extra 'bit' of state that every object with a finalizer has.考虑 Java 最终确定的最简单方法是将其视为 state 的额外“位”,每个 object 都具有终结器。 When a new object is created, this isFinalized bit is set to false.创建新的 object 时,此isFinalized位设置为 false。 When the garbage collector finds that an object with a finalizer is unreachable, it checks this isFinalized bit and only reclaims the object if it is true -- if it is false it instead runs the finalizer and sets the bit to true.当垃圾收集器发现带有终结器的 object 无法访问时,它会检查这个isFinalized位,如果它是真的,它只会回收 object - 如果它是假的,它会运行终结器并将该位设置为真。 Once set, there's no way for the bit to ever be cleared, so in any later time the garbage collector runs, if it's unreachable, it will be collected.一旦设置,就无法清除该位,因此在垃圾收集器运行后的任何时间,如果它无法访问,它将被收集。

It is not entirely clear what you are asking.您在问什么并不完全清楚。

However, the JLS 12.6.2 states that the order in which objects are finalized is not specified.但是, JLS 12.6.2声明未指定对象最终确定的顺序。 This means that the finalize methods for all finalizable classes should be designed to work when invoked in any order.这意味着所有可终结类的终结方法都应该设计为在以任何顺序调用时都可以工作。

Note that this applies equally to unreachable and finalizer-reachable objects.请注意,这同样适用于不可访问对象和终结器可访问对象。 In other words, the 3 objects in your diagram could be finalized in any order.换句话说,图中的 3 个对象可以按任何顺序完成。


... how B will be garbage collected as A is reachable and how finalize will get called for B? ... B 将如何被垃圾收集,因为 A 是可到达的,以及如何为 B 调用 finalize?

Perhaps it is a trick question.也许这是一个技巧问题。 If A is reachable, then B is also reachable, so it won't be garbage collected, and its finalize method won't be called.如果 A 是可达的,那么 B 也是可达的,所以不会被垃圾回收,也不会调用它的 finalize 方法。 If A becomes unreachable then so does B, and both will be finalized.如果 A变得不可达,那么 B 也变得不可达,并且两者都将被最终确定。

What that finalize method will actually do depends on what class it belongs to.该 finalize 方法实际上会做什么取决于它所属的 class 。 Lets assume it is a method of the "guaranteed reachable object", and the a variable contains a reference to A:假设它是“保证可达对象”的方法,并且a变量包含对 A 的引用:

  • The finalize method won't be called by the GC because the object is reachable. GC 不会调用 finalize 方法,因为 object 是可访问的。

  • Some other code could explicitly call that finalize method.其他一些代码可以显式调用该finalize方法。 If that happened, then ab will no longer refer to B, and B will be unreachable and eligible for eventual garbage collection, finalization and (ultimately) deletion.如果发生这种情况,那么ab将不再引用 B,并且 B 将无法访问并且有资格进行最终的垃圾收集、终结和(最终)删除。

Sorry if I am wrong.对不起,如果我错了。 But to me, B is a field of A, so A definitely need to keep a reference to B, and while A "always-live" object holding a reference to A, both A and B will not be GC and the problem to GC B is just not making any sense.但是对我来说,B 是 A 的一个字段,所以 A 肯定需要保留对 B 的引用,而 A “永远存在” object 持有对 A 的引用,A 和 B 都不会是 GC 和 GC 的问题B只是没有任何意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM