我的代码如下所示:
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
阻止,这是很多重复的代码)。
答案 0 :(得分:1)
不,如果不使用try...catch
阻止,则无法实现此目的,请参阅jls:
如果找不到可以处理异常的catch子句,那么 当前线程(遇到异常的线程)是 终止。
并且,我不认为缓存线程池中的线程终止是一个问题,因为下次提交新任务时,将创建一个新线程来处理它。
如果真的很重要,而且你不想重复代码,你可以编写一个这样的包装类:
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);