public class TestHook {
public static boolean hookFlag = false;
public static void main(String[] args) {
Thread shutDownThread = new Thread() {
public void run() {
TestHook.hookFlag = true;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("hook success!"+TestHook.hookFlag);
}
};
Runtime.getRuntime().addShutdownHook(shutDownThread);
System.out.println("hook init" + TestHook.hookFlag);
while(!TestHook.hookFlag) {
try {
Thread.sleep(1000);
System.out.println("没安全退出");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("安全退出");
}
}
执行命令 kill [pid]
原文地址:http://blog.51cto.com/jtech/2116635