如何在没有try catch的情况下抛出异常时避免线程池中的线程死亡

How to avoid thread in threadpool dead when exception thrown without try catch

我的代码如下所示:

public class ExceptionTest {
    public static Logger log = LoggerFactory.getLogger(ExceptionTest.class);
    public final static ThreadFactory factory = new ThreadFactory() {
        @Override
        public Thread newThread(Runnable target) {
            final Thread thread = new Thread(target);
            log.debug("Creating new worker thread");
            thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    log.error("Uncaught Exception", e);
                }
            });
            return thread;
        }

    };
    final static ExecutorService executor = Executors.newCachedThreadPool(factory);
    public static void main(String[] args) {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName());
                    int i = 1;
                    int j = 0;
                    System.out.println(i / j);
                }
            }
        });
    }

}

控制台只打印一次消息。这意味着线程已经死亡。有没有其他方法可以防止线程死亡(除了try catch块,这是很多重复的代码)。

不,如果不使用 try...catch 块,您将无法实现此目的,请参阅 jls:

If no catch clause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated.


而且,我认为缓存线程池中的线程终止不是问题,因为下次您提交新任务时,将创建一个新线程来处理它。


如果它真的很重要,并且你不想重复代码,你可以像这样写一个包装器class:

public class WrapperRunnable implements Runnable {

    Runnable runnable;

    public WrapperRunnable(Runnable runnable) {
        this.runnable = runnable;
    }

    @Override
    public void run() {
        while (true) {
            try {
                runnable.run();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

并提交WrapperRunnable给执行者:

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
        int i = 1;
        int j = 0;
        System.out.println(i / j);
    }
};
WrapperRunnable wrapperRunnable = new WrapperRunnable(runnable);
executor.execute(wrapperRunnable);