线程池在提交的任务在处理过程中发生了异常,却没有捕获到,导致异常只是输出在控制台,这通常需要把异常记录下来
1、通过观察ThreadGroup的构造方法知道,当调用线程组的构造方法时,会获取当前线程所属的线程组,作为当前正在创建的线程组的parent线程组。
2、通过在main方法中获取线程组信息可知,当前main线程所属的线程组是main线程组,而main线程组的parent线程组是system线程组。
3、ThreadGroup的uncaughtException实现如下
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);
}
}
}
4、Thread类有个dispatchUncaughtException方法,当Thread线程运行run方法时,异常抛出来而没有处理时将会把异常抛给虚拟机,虚拟机将会交给该Thread对象的dispatchUncaughtException方法处理,可以从如下代码看到,如果没有给Thread设置具体的UncaughtExceptionHandler,将会给线程组处理,而线程组一直委托给parent处理,所以最后到了system线程组处理,由于它的parent是null,所以就使用System.err输出了异常信息
private void dispatchUncaughtException(Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler != null ?uncaughtExceptionHandler : group;
}
5、线程池中默认的DefaultThreadFactory的实现如下
static class DefaultThreadFactory implements ThreadFactory {
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
/* 交给的是当前线程所属的线程组 */
group = (s != null) ? s.getThreadGroup() :
Thread.currentThread().getThreadGroup();
namePrefix = "pool-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
/* 创建的线程 所指定的线程组是在构造方法中设置的 */
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
if (t.isDaemon())
t.setDaemon(false);
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
测试
public class ThrTask implements Runnable {
private int a, b;
private ThrTask(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public void run() {
double re = a / b;
System.out.println(re);
}
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 1, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, "myThread");
if (t.isDaemon()) t.setDaemon(false);
t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("线程" + t.getName() + "出现异常" + e);
// record this self..
}
});
return t;
}
});
for (int i = 0; i < 5; i++) {
//将不会抛出异常
// threadPoolExecutor.submit(new ThrTask(5, i));
//将会抛出异常
threadPoolExecutor.execute(new ThrTask(5, i));
}
}
}