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.
分享到:
相关推荐
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 ...
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 ...
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 ...