`
yaojingguo
  • 浏览: 213260 次
  • 性别: 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.
分享到:
评论

相关推荐

    Java邮件开发Fundamentals of the JavaMail API

    framework adds support for typing arbitrary blocks of data and handling it accordingly. This doesn't sound like much, but it is your basic MIME-type support found in many browsers and mail tools ...

    Scipy - Scipy Lecture Notes

    Python emphasizes code readability and allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. **2.2 Basic Types** Python includes ...

    python3.6.5参考手册 chm

    What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations PEP 515: Underscores in ...

Global site tag (gtag.js) - Google Analytics