如何finalize方法和shutdownHooks互動

 public class ShutdownDemo {
  public static void main(String[] argsthrows Exception {

    // Create an Object with a finalize() method.
    Object f = new Object() {
      public void finalize() {
        System.out.println"Running finalize()");
      }
    };

    // Add a shutdownHook to the JVM
    Runtime.getRuntime().addShutdownHook(new Thread() {
      public void run() {
        System.out.println("Running Shutdown Hook");
      }
    });

    // Unless the user puts -f (for "free") on the command line,
    // call System.exit while holding a reference to 
    // Object f, which can therefore not be finalized().

    if (args.length == && args[0].equals("-f")) {
      f = null;
      System.gc();
    }

    System.out.println("Calling System.exit()");
    System.exit(0);
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章