zoukankan      html  css  js  c++  java
  • Effective Java 07 Avoid finallizers

    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.

       

    作者:小郝
    出处:http://www.cnblogs.com/haokaibo/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    俞敏洪:从马云、柳传志、王健林、马化腾看人与人的区别
    张瑞敏:人不成熟的五大特征
    为那么些在毕业季迷茫的人们,送上马云在清华经管院的毕业典礼演讲
    HTML5学习+javascript学习:打飞机游戏Service层Control层+源码
    HTML5学习+javascript学习:打飞机游戏简介以及Model层
    javascript的笔记
    搜狗主页页面CSS学习小记
    简单账务管理系统源码
    Mac SSH免密码登录 Linux服务器
    Flink写入数据到Elasticsearch示例
  • 原文地址:https://www.cnblogs.com/haokaibo/p/avoid-finallizers.html
Copyright © 2011-2022 走看看