如何确保始终调用 finalize()(Java 练习中的思考)
我正在慢慢研究 Bruce Eckel 的 Thinking in Java 第 4 版,以下问题让我很困惑:
I'm slowly working through Bruce Eckel's Thinking in Java 4th edition, and the following problem has me stumped:
使用 finalize( ) 方法创建一个打印消息的类.在 main( ) 中,创建你的类的一个对象.修改前面的练习,以便始终调用您的 finalize().
Create a class with a finalize( ) method that prints a message. In main( ), create an object of your class. Modify the previous exercise so that your finalize( ) will always be called.
这是我写的代码:
public class Horse {
boolean inStable;
Horse(boolean in){
inStable = in;
}
public void finalize(){
if (!inStable) System.out.print("Error: A horse is out of its stable!");
}
}
public class MainWindow {
public static void main(String[] args) {
Horse h = new Horse(false);
h = new Horse(true);
System.gc();
}
}
它创建一个新的 Horse
对象,布尔值 inStable
设置为 false
.现在,在 finalize()
方法中,它检查 inStable
是否为 false
.如果是,它会打印一条消息.
It creates a new Horse
object with the boolean inStable
set to false
. Now, in the finalize()
method, it checks to see if inStable
is false
. If it is, it prints a message.
很遗憾,没有打印任何消息.由于条件评估为 true
,我的猜测是 finalize()
一开始就没有被调用.我已经多次运行该程序,并且只看到错误消息打印了几次.我的印象是,当调用 System.gc()
时,垃圾收集器将收集任何未引用的对象.
Unfortunately, no message is printed. Since the condition evaluates to true
, my guess is that finalize()
is not being called in the first place. I have run the program numerous times, and have seen the error message print only a couple of times. I was under the impression that when System.gc()
is called, the garbage collector will collect any objects that aren't referenced.
谷歌搜索一个正确的答案给了我 这个链接,它提供了更详细的信息,复杂的代码.它使用了我以前从未见过的方法,例如 System.runFinalization()
、Runtime.getRuntime()
和 System.runFinalizersOnExit()
.
Googling a correct answer gave me this link, which gives much more detailed, complicated code. It uses methods I haven't seen before, such as System.runFinalization()
, Runtime.getRuntime()
, and System.runFinalizersOnExit()
.
有没有人能让我更好地理解 finalize()
的工作原理以及如何强制它运行,或者引导我了解解决方案代码中的操作?
Is anybody able to give me a better understanding of how finalize()
works and how to force it to run, or walk me through what is being done in the solution code?
推荐答案
当垃圾收集器找到一个符合收集条件但具有 finalizer
的对象时,它不会立即释放它.垃圾收集器尝试尽快完成,因此它只是将对象添加到具有未决终结器的对象列表中.终结器稍后在单独的线程上调用.
When the garbage collector finds an object that is eligible for collection but has a finalizer
it does not deallocate it immediately. The garbage collector tries to complete as quickly as possible, so it just adds the object to a list of objects with pending finalizers. The finalizer is called later on a separate thread.
您可以通过调用 System.runFinalization
垃圾回收之后.
You can tell the system to try to run pending finalizers immediately by calling the method System.runFinalization
after a garbage collection.
但如果你想强制运行终结器,你必须自己调用它.垃圾收集器不保证任何对象将被收集或终结器将被调用.它只会尽力而为".但是,您很少需要强制终结器在实际代码中运行.
But if you want to force the finalizer to run, you have to call it yourself. The garbage collector does not guarantee that any objects will be collected or that the finalizers will be called. It only makes a "best effort". However it is rare that you would ever need to force a finalizer to run in real code.
相关文章