目录

boolean removeShutdownHook(Thread hook)

描述 (Description)

java.lang.Runtime.removeShutdownHook(Thread hook)方法取消注册先前注册的虚拟机关闭挂钩。

声明 (Declaration)

以下是java.lang.Runtime.removeShutdownHook()方法的声明

public boolean removeShutdownHook(Thread hook)

参数 (Parameters)

hook - 要删除的钩子

返回值 (Return Value)

如果指定的挂钩先前已注册并已成功取消注册,则此方法返回true否则返回false

异常 (Exception)

  • IllegalStateException - 如果虚拟机已在关闭的过程中

  • SecurityException - 如果存在安全管理器并且它拒绝RuntimePermission(“shutdownHooks”)

例子 (Example)

以下示例显示了lang.Runtime.removeShutdownHook()方法的用法。

package com.iowiki;
public class RuntimeDemo {
   // a class that extends thread that is to be called when program is exiting
   static class Message extends Thread {
      public void run() {
         System.out.println("Bye.");
      }
   }
   public static void main(String[] args) {
      try {
         Message p = new Message();
         // register Message as shutdown hook
         Runtime.getRuntime().addShutdownHook(p);
         // print the state of the program
         System.out.println("Program is starting...");
         // cause thread to sleep for 3 seconds
         System.out.println("Waiting for 3 seconds...");
         Thread.sleep(3000);
         // remove the hook
         Runtime.getRuntime().removeShutdownHook(p);
         // print that the program is closing 
         System.out.println("Program is closing...");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Program is starting...
Waiting for 3 seconds...
Program is closing...
↑回到顶部↑
WIKI教程 @2018