NOTE

  1. Never do anything time-critical in a finalizer.
  2. Never depend on a finalizer to update critical persistent state.
  3. There is a severe performance penalty for using.
  4. "Finalizer chaining" is not performed automatically. The subclass finalizer must invoke the superclass finalizer manually.
    1. Manual finalizer chaining

    @Override

    protected void finalize() throws Throwable {

    try {

    ... // Finalize subclass state

    } finally {

    super.finalize();

    }

    }

    b. Finalizer Guardian idiom

    // Finalizer Guardian idiom. This will prevent the subclass forgets to invoke super class's finalize method.

    public class Foo {

    // Sole purpose of this object is to finalize outer Foo object

    private final Object finalizerGuardian = new Object() {

    @Override protected void finalize() throws Throwable {

    ... // Finalize outer Foo object

    }

    };

    ... // Remainder omitted

    }

What to do

  1. Provide an explicit termination method

the explicit termination method must record in a private field that the object is no longer valid, and other methods must check this field and throw an IllegalStateException if they are called after the object has been terminated.

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

    // try-finally block guarantees execution of termination method

    Foo foo = new Foo(...);

    try {

    // Do what must be done with foo

    ...

    } finally {

    foo.terminate(); // Explicit termination method

    }

Usage of finallizer

  1. Safety net: ensure the resources being released even if the explicitly termination method is not executed correctly.

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

  2. Concerns objects with native peers. A native peer is a native object to which a normal object delegates via native methods. Because a native peer is not a normal object, the garbage collector doesn't know about it and can't reclaim it when its Java peer is reclaimed. A finalizer is an appropriate vehicle for performing this task, assuming the native peer holds no critical resources.

Summary

In summary, don't use finalizers except as a safety net or to terminate noncritical native resources. In those rare instances where you do use a finalizer, remember to invoke super.finalize. If you use a finalizer as a safety net, remember to log the invalid usage from the finalizer. Lastly, if you need toassociate a finalizer with a public, nonfinal class, consider using a finalizer guardian, so finalization can take place even if a subclass finalizer fails to invoke super.finalize.

Effective Java 07 Avoid finallizers的更多相关文章

  1. Effective Java 67 Avoid excessive synchronization

    Principle To avoid liveness and safety failures, never cede control to the client within a synchroni ...

  2. Effective Java 50 Avoid strings where other types are more appropriate

    Principle Strings are poor substitutes for other value types. Such as int, float or BigInteger. Stri ...

  3. Effective Java 73 Avoid thread groups

    Thread groups were originally envisioned as a mechanism for isolating applets for security purposes. ...

  4. Effective Java 05 Avoid creating unnecessary objects

    String s = new String("stringette"); // Don't do this. This will create an object each tim ...

  5. Effective Java 48 Avoid float and double if exact answers are required

    Reason The float and double types are particularly ill-suited for monetary calculations because it i ...

  6. Effective Java 59 Avoid unnecessary use of checked exceptions

    The burden is justified if the exceptional condition cannot be prevented by proper use of the API an ...

  7. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  8. Effective Java 创建和销毁对象

    <Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...

  9. Effective Java 第三版——22. 接口仅用来定义类型

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

随机推荐

  1. List<?>和List<T>的区别?

    出自:https://www.zhihu.com/question/31429113

  2. C#中往数据库插入/更新时候关于NUll空值的处理

    本文转载:http://blog.csdn.net/chybaby/article/details/2338943 本文转载:http://www.cnblogs.com/zfanlong1314/a ...

  3. ScriptManager和UpdatePanel控件实现局部刷新

    ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果.其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因 ...

  4. js验证电话号码的正则表达式

    在做程序时遇到js验证电话号码的问题,使用正则表达式来操作很简单的.一起来看一下吧. 1,这种是比较简单的验证号码: 电话号码只能包含”(“.“)”.“-”和数字 <input   type=t ...

  5. Jquery Validation 多按钮,多表单,分组验证

    真正做到了 多按钮的验证. 在用户输入的时候就可以验证,而网上大部分多按钮验证都是必须要用户点击按钮后才可以验证. 研究了两天终于弄出来了,不知道两天是过长还是过段,现在分享给小伙伴们. 小伙伴们支持 ...

  6. PHP OAuth2 Server库

    想找比较正宗的库,查了蛮久的.最后在 oauth官方站上,看到PHP版本的相关链接. 发现都是php 5.3版本以上的环境,基于命名空间的写法编写的. 访问下面这个页面,难得,发现文档给出了5.2版本 ...

  7. [moka同学笔记]yii2.0 advanced高级版 安装配置 与 rbac (Ⅰ)

    1.下载地址:http://www.yiichina.com/download,下载 Yii2 的高级应用程序模板 2.配置与安装 在服务器www目录下yii2test  [下载下来更改advance ...

  8. linux系统如何将系统中的文件名改为英文?

    由于我们经常在命令行模式下进入文件,那么中英文的切换常常会影响我们输入的效率. 那么如何将原来的中文修改成英文的字幕呢? 如下图所示: -------------------------------- ...

  9. hibernate映射文件one-to-one

    one-to-one 元素 属性: name:映射类属性的名字 class:映射的目标类 cascade:设置操作中的级联策略 可选值为 all所有操作情况均进行级联.none所有操作情况均不进行级联 ...

  10. CentOS7 Debian 8 安装VMware-tools

    如在安装过程中碰到未找到gcc 或者 kernel headers的可按以下方案解决,适用任意版本 CentOS 7 1. Update the kernel: $ yum update kernel ...