簡體   English   中英

終結者守護者未運行

[英]finalizer guardian didn't run

在我的程序退出時,我的終結器守護程序無法運行。

他在這里:

public class SomeClass implements SomeInterface {

    ... setup the PrintWriter os somewhere here

    /** oh, i guess i wanted to try out finalizer guardians here. */
    @SuppressWarnings("unused")
    private final Object finalizerGuardian = new Object(){
        protected void finalize(){
            try {
                //actually, i cannot know if it was closed or not. 
                System.out.println("connection wasn't closed - it needs to be!");
                os.flush();
                os.close();
            } catch (Exception se){
                System.out.println("some sort of exception occurred? Weird");
            }
        }
    };

    ...

}

我做錯了什么? 我以為finalizerGuardians可以保證運行? 還是不是這樣? 到該程序終止時,肯定不會刷新或關閉PrintWriter操作系統。

在垃圾回收期間將調用finalize() ,您的程序可能在此之前就已存在。

終結器線程是垃圾回收(GC)的一部分。 盡管確實是GC的一部分,但GC不能保證它何時運行。 Finalize方法將對象移至終結器對象隊列,終結器線程在運行時清除終結器中存在的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM