Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("做点事情");
}));
当我们在执行一段程序的时候,程序被打断了的时候 ,我们可以通过上面的方法来完成一些其他的事情,例如发送邮件告知等
public static String age(){
Runtime.getRuntime().addShutdownHook(new Thread(()->{
System.out.println("做点事情");
}));
String a = null;
int size = a.length();
return a;
}
当程序执行到 a.length()的时候 会出现一个NullPointerExeception 这个时候程序会终止。但是通过线程的钩子函数我们还是能做一些其他的事情的
1061

被折叠的 条评论
为什么被折叠?



