Signal Handling in Java

class Hook1 extends Thread {
  @Override
  public void run() {
    System.out.println("hook1");  
  }
}

class Hook2 extends Thread {
  @Override
  public void run() {
    System.out.println("hook2");  
  }
}

public class HookDemo  {
   public static void main (String [] args) throws Exception {
     System.out.println("start");  
     Runtime.getRuntime().addShutdownHook(new Hook1());
     Runtime.getRuntime().addShutdownHook(new Hook2());
     Thread.sleep(200 * 1000);
   }
}
 Compile it and run "java HelloDemo". Issue Ctrl+C. These two hooks will be invoked. "kill -TERM" and "Kill -INT" will also trigger the hooks. But "kill -KILL" will not.

你可能感兴趣的:(Signal)