在没有向线程注入 UncaughtExceptionHandler 回调接口的情况下,线程若出现了异常又该如何处理,下面通过源码追踪下。
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler != null ?
uncaughtExceptionHandler : group;
}
getUncaughtExceptionHandler 首先会判断当前线程是否设置了 handler,如果有则执行自己的 uncaughtExceptionHandler 方法,否则就到所在的 ThreadGroup 中获取,ThreadGroup 同样也实现了 UncaughtExceptionHandler 接口。
public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread \""
+ t.getName() + "\" ");
e.printStackTrace(System.err);
}
}
}
package concurrent;
import java.util.concurrent.TimeUnit;
public class EmptyExceptionhandler {
public static void main(String[] args) {
ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();
System.out.println(mainGroup.getName());
System.out.println(mainGroup.getParent());
System.out.println(mainGroup.getParent().getParent());
Thread thread = new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(1/0);
},"Test-Thread");
thread.start();
}
}
main
java.lang.ThreadGroup[name=system,maxpri=10]
null
Exception in thread "Test-Thread" java.lang.ArithmeticException: / by zero
at concurrent.EmptyExceptionhandler.lambda$main$0(EmptyExceptionhandler.java:18)
at java.lang.Thread.run(Thread.java:748)
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/chengqiuming/article/details/123829603
内容来源于网络,如有侵权,请联系作者删除!