zoukankan      html  css  js  c++  java
  • Effective Java 英文 第二版 读书笔记 Item 7:Avoid finalizers

    Finalizers are unpredictable,often dangerous,and generally unnecessary,in java.

    may cause poor ferformance,instability behavior,and portability(移植)  problems.

    so avoid finalizers as as possible.

    never do anything time-critical in a finalizer,

    for example: 

    A colleague debugged a long-running GUI application that was mysteriously dying with an OutOfMemoryError.

    Analysis revealed that at the time of its death,the application had thousands of graphics objects on its finalizer queue just waiting to be finalized and reclaimed. 

    Unfortunately,the finalizer thread was running at a lower priority than another application thread.

    同事debug一个长时间运行的图形应用报错OutOfMemoryError(内存不足错误),

    分析发现对象死亡的同时,这个应用仍然数千个图形对象在其finalizer队列等待被终结、回收,

    不幸运的是,finalizer线程优先级低于应用其他线程

    程序不能保证finalizers的执行,当显式调用时。

    never depend on a finalizer to update critical persistent state

    instead of writing a finalizer for a class whose objects encapsulate resources that require termination,

    just provide an explicit termination method,

    比如数据流和数据库连接的关闭

    Explicit termination methods are typically used in combination with the try-finally construct to ensure termination.

    利用finally保证执行关闭

    the finalizer should log a warning if it finds that the resource has not been terminated

  • 相关阅读:
    oo第二次博客作业
    oo第一次博客作业
    软件工程第3次作业 | 提问回顾与个人总结
    软件工程第2次作业 | 结对项目-最长单词链
    软件工程第1次作业 | 第一次阅读
    软件工程第0次作业 | 热身
    OO第四次博客作业
    OO第三次博客作业
    OO第二次博客作业
    OO第一次博客作业
  • 原文地址:https://www.cnblogs.com/linkarl/p/5527609.html
Copyright © 2011-2022 走看看