java 中的finally{}块与finalize()方法

finalize()方法,

    /**
     * Called by the garbage collector on an object when garbage collection
     * determines that there are no more references to the object.
     * A subclass overrides the <code>finalize</code> method to dispose of
     * system resources or to perform other cleanup. 
     * <p>
     * The general contract of <tt>finalize</tt> is that it is invoked 
     * if and when the Java<font size="-2"><sup>TM</sup></font> virtual 
     * machine has determined that there is no longer any
     * means by which this object can be accessed by any thread that has
     * not yet died, except as a result of an action taken by the
     * finalization of some other object or class which is ready to be
     * finalized. The <tt>finalize</tt> method may take any action, including
     * making this object available again to other threads; the usual purpose
     * of <tt>finalize</tt>, however, is to perform cleanup actions before 
     * the object is irrevocably discarded. For example, the finalize method 
     * for an object that represents an input/output connection might perform
     * explicit I/O transactions to break the connection before the object is
     * permanently discarded. 
     * <p>
     * The <tt>finalize</tt> method of class <tt>Object</tt> performs no 
     * special action; it simply returns normally. Subclasses of 
     * <tt>Object</tt> may override this definition.
     * <p>
     * The Java programming language does not guarantee which thread will 
     * invoke the <tt>finalize</tt> method for any given object. It is 
     * guaranteed, however, that the thread that invokes finalize will not 
     * be holding any user-visible synchronization locks when finalize is 
     * invoked. If an uncaught exception is thrown by the finalize method, 
     * the exception is ignored and finalization of that object terminates.
     * <p>
     * After the <tt>finalize</tt> method has been invoked for an object, no 
     * further action is taken until the Java virtual machine has again 
     * determined that there is no longer any means by which this object can 
     * be accessed by any thread that has not yet died, including possible
     * actions by other objects or classes which are ready to be finalized, 
     * at which point the object may be discarded.
     * <p>
     * The <tt>finalize</tt> method is never invoked more than once by a Java
     * virtual machine for any given object.
     * <p>
     * Any exception thrown by the <code>finalize</code> method causes 
     * the finalization of this object to be halted, but is otherwise 
     * ignored. 
     *
     * @throws Throwable the <code>Exception</code> raised by this method
     */
    protected void finalize() throws Throwable { }

看完注释,就让我们逐条分析一下:

    finalize()是Object类的一个protected 方法,方法体为空。

1)什么时候执行finalize()方法呢?


  

  当对象覆盖了finalize()方法且对象在进行可达性分析后发现没有与GC root相连接的的引用链且对象的finalzie()没有被执行,jvm调用finalize()方法。

2)如何执行finalize()方法?



     如果这个对象本判定为有必要执行finalize()方法,,那么这个对象就会放置在一个叫做F-Queue的队列中,并在稍后由一个虚拟机自动建立的,低优先级的Finalizer线程去执行它。这里说所谓的“执行”是指虚拟机会触发这个方法,但并不承诺会等待它运行结束,这样做的原因是,如果一个对象在finalize()方法中执行缓慢,或者发生死循环,将可能会导致F-Queue队列中其他对象永久等待,甚至导致整个内存回收系统崩溃。当finalize()发生异常,该对象停止,不会影响其他对象。

3)需要注意的:

 finalze() 仅执行一次。

4)建议: 

    建议大家尽量避免使用它,它不是C/C++中的析构函数,而是java诞生时为了使c/c++程序员更容易接受它所做的一个妥协。它的运行代价高昂,不确定性大,无法保证各个对对象的调用顺序。虽然注释中描述它适合做“关闭外部资源”之类的工作,这完全是对这个方法用途的一种安慰,finalize()能做的所用工作,使用try-finally或者其他方式都可以做的更好,更及时,所以建议大家完全可以忘掉java语言中有这个方法。


2 finally {}




 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值