下面是学习如何捕获多线程异常的一个范例:
import java.util.concurrent.*;class ExceptionThread2 implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.println("run() by " + t);
System.out.println(
"eh = " + t.getUncaughtExceptionHandler());
throw new RuntimeException();
}
}class MyUncaughtExceptionHandler implements
Thread.UncaughtExceptionHandler {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("caught " + e);
}
}class HandlerThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
System.out.println(this + " creating new Thread");
Thread t = new Thread(r);
System.out.println("created " + t);
t.setUncaughtExceptionHandler(
new MyUncaughtExceptionHandler());
System.out.println(
"eh = " + t.getUncaughtExceptionHandler());
return t;
}
}public class CaptureUncaughtException {
public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool(
new HandlerThreadFactory());
exec.execute(new ExceptionThread2());
}
}
输出是:
chapter21.chapter21_1.HandlerThreadFactory@6d6f6e28 creating new Thread
created Thread[Thread-0,5,main]
eh = chapter21.chapter21_1.MyUncaughtExceptionHandler@135fbaa4
run() by Thread[Thread-0,5,main]
eh = chapter21.chapter21_1.MyUncaughtExceptionHandler@135fbaa4
chapter21.chapter21_1.HandlerThreadFactory@6d6f6e28 creating new Thread
created Thread[Thread-1,5,main]
eh = chapter21.chapter21_1.MyUncaughtExceptionHandler@7c5085ec
caught java.lang.RuntimeException
问题1:看输出结果是创建了两个线程,但明明只有一个exec.execute(new ExceptionThread2());为什么会创建两个线程
问题2:当输出caught java.lang.RuntimeException后程序并没有退出,而是一直在运行状态,但也没有任何输出,只能手动关闭,为什么?

解决方案 »

  1.   

    问题1解释:
    之所以产生2个线程,这个跟线程池自身提供异常处理机制有关:线程执行异常时创建一个新的线程替换(非绝对)
    如何验证:源码说明一切,建议楼主有时间可以研究下ThreadPoolExecutor的源码,网上很多人对线程池的源码做了很好的分析,可以网上查下辅助源码阅读,这里不多做详述。private void processWorkerExit(Worker w, boolean completedAbruptly) {
            if (completedAbruptly) // If abrupt, then workerCount wasn't adjusted
                decrementWorkerCount();        final ReentrantLock mainLock = this.mainLock;
            mainLock.lock();
            try {
                completedTaskCount += w.completedTasks;
                workers.remove(w);
            } finally {
                mainLock.unlock();
            }        tryTerminate();        int c = ctl.get();
            if (runStateLessThan(c, STOP)) {
                if (!completedAbruptly) {
                    int min = allowCoreThreadTimeOut ? 0 : corePoolSize;
                    if (min == 0 && ! workQueue.isEmpty())
                        min = 1;
                    if (workerCountOf(c) >= min)
                        return; // replacement not needed
                }
                addWorker(null, false); //创建一个空任务的工作线程
            }
        }问题2解释:
    由于自定义的线程工厂创建的线程都是非守护线程,当main线程执行结束后,程序是不会结束的(存在非守护线程还在执行)