public class TestMe {
static boolean isNornamExit = true;
private static final void shutdownCallback() {
if (!isNornamExit) {
System.out.println("exception.");
} else {
System.out.println("exit normally.");
}
}
public static void main(String[] args) throws InterruptedException {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
shutdownCallback();
}
});
isNornamExit = false;
System.exit(1);
}
}
注意正常退出也会执行 shutdownCallback()方法,所以要有标识存储是否正常退出信息,然后根据标识决定hook 方法怎么实现。
--end