package com.thread.hook;
/**
* 给应用程序注入钩子程序
*/
public class ExitCapture {
public static void main(String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("程序将要退出");
notifyAndRelease();
}));
int i = 0;
while (true){
try {
Thread.sleep(1_000);
System.out.println("working.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
if (i==15){
throw new RuntimeException("error");
}
}
}
public static void notifyAndRelease(){
System.out.println("notify to the admin");
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程将要释放资源");
try {
Thread.sleep(1_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("已经释放资源");
}
}
给应用程序注入钩子程序
最新推荐文章于 2023-06-09 00:31:44 发布