ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setNameFormat("httpclient-pool-%d")
.setDaemon(true)
.setUncaughtExceptionHandler((t, e)-> {
System.out.println("----------");
System.out.println(t.getName() +":"+ e.getMessage());
})
.build();
ExecutorService executorService = Executors.newFixedThreadPool(10, threadFactory);
executorService.submit() 时,setUncaughtExceptionHandler 无效,需要 future.get(); 的方式获得异常。
executorService.excute() 时,setUncaughtExceptionHandler 可以捕获异常。
网友评论