package player.kent.chen.learn.sdh;
public class HelloShutdownHook {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println(Thread.currentThread().getName() + "-钩子,再见1");
}
});
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
System.out.println(Thread.currentThread().getName() + "-钩子,再见2");
}
});
System.out.println(Thread.currentThread().getName() + "-Hello, world!");
}
}
输出:
引用
main-Hello, world!
Thread-1-钩子,再见2
Thread-0-钩子,再见1
可见:
1. 系统退出时会执行hook里面的操作
2. 这些操作的执行是在独立线程里执行的,而且是异步的