`
yaojingguo
  • 浏览: 207875 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Signal Handling in Java

    博客分类:
  • 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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics