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. Mysql学习笔记(十二)触发器

    学习内容: 1.触发器: 什么是触发器?我们什么时候能够使用触发器?   触发器就是用来监听某个表的变化,当这个表发生变化的时候来触发某种操作..比若说两个表是相互关联的,当我们在对其中一个表格进行操 ...

  2. mobile 更改hosts

    在Android下,/etc是link到/system/etc的,我们需要修改/system/etc/hosts来实现.但是这个文件是只读,不能通过shell直接修改.可以通过连接到PC上使用adb来 ...

  3. SystemTap知识(一)

    SystemTap是一个系统的跟踪探测工具.它能让用户来跟踪和研究计算机系统在底层的实现. 安装SystemTap需要为你的系统内核安装-devel,-debuginfo,-debuginfo-com ...

  4. dp --- CSU 1547: Rectangle

    Rectangle Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1547 Mean: 给你一些宽为1或2 的木 ...

  5. Linq查询操作之投影操作

    投影操作,乍一看不知道在说啥.那么什么是投影操作呢?其实就是Select操作,名字起的怪怪的.和Linq查询表达式中的select操作是一样的.它能够选择数据源中的元素,并指定元素的表现形式.投影操作 ...

  6. C#调用windows api示例

    这是运行结果: Api函数是构筑Windws应用程序的基石,每一种Windows应用程序开发工具,它提 供的底层函数都间接或直接地调用了Windows API函数,同时为了实现功能扩 展,一般也都提供 ...

  7. Ext.GridPanel 用法总结(一)—— Grid基本用法

    Ext.GridPanel 用法总结(一)—— Grid基本用法 摘自:http://www.cnblogs.com/luluping/archive/2009/08/01/1536645.html ...

  8. 自定义动画方法animate

    animate的使用方法:animate(params,speed,callback); 例子:animate({ right: "-=600px",height:"+= ...

  9. ASP.NET MVC进阶一

    一.控制器相关 在Controller类中方法访问级别为public的方法,就是行为(Action). 如果不希望Controller类中的方法成为Action(可以在地址栏中被访问),有两种实现方式 ...

  10. java jdk environment variables

    1. create system variable 2. edit the system path note: ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; 3. cre ...