如何确保finalize()总是被调用(Thinking in Java exercise) [英] How to ensure finalize() is always called (Thinking in Java exercise)

查看:267
本文介绍了如何确保finalize()总是被调用(Thinking in Java exercise)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

$ b $

我正在慢慢地研究Bruce Eckel的 in Java 4th edition b

使用可打印消息的finalize()方法创建一个类。在main()中,创建你的类的一个对象。修改前面的练习,以便始终调用finalize()。


这是我编写的代码:

  public class Horse {
boolean inStable;
马(boolean in){
inStable = in;
$ b $ public void finalize(){
if(!inStable)System.out.print(Error:A horse is its stable!);


public class MainWindow {
public static void main(String [] args){
Horse h = new Horse(false);
h =新马(true);
System.gc();


它创建一个新的马对象的布尔型 inStable 设置为 false 。现在,在 finalize()方法中,它检查 inStable 是否为 false 。如果是,它会打印一条消息。

不幸的是,没有消息被打印。由于条件评估结果为 true ,我的猜测是首先不会调用 finalize()。我已经多次运行该程序,并且只看到几次打印的错误消息。当我调用 System.gc()时,垃圾收集器将收集任何未被引用的对象。



使用Google搜索正确答案给了我这个链接,它给出了更详细,复杂的代码。它使用我以前从未见过的方法,比如 System.runFinalization() Runtime.getRuntime(),和 System.runFinalizersOnExit()



有人能够让我更好地理解 finalize()的工作方式,以及如何强制它运行,或者引导我完成解决方案代码中的工作?

解决方案

当垃圾收集器发现有资格收集但具有终结器的对象时,它不会立即释放它。垃圾收集器尽可能快地完成,因此它只是将对象添加到具有挂起的终结器的对象列表中。终止程序稍后会在单独的线程中调用。



您可以通过调用方法 System.runFinalization 在垃圾收集之后。



但是,如果您想要 force 终结器运行,您必须自己调用它。垃圾回收器不会保证收集任何对象或调用终结器。它只做一个尽力而为。但是很少有人需要强制终结器以真实代码运行。


I'm slowly working through Bruce Eckel's Thinking in Java 4th edition, and the following problem has me stumped:

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.

This is what I have coded:

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();
    }
}

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.

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.

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().

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?

解决方案

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.

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.

这篇关于如何确保finalize()总是被调用(Thinking in Java exercise)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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