美文网首页
thread UncaughtExceptionHandler

thread UncaughtExceptionHandler

作者: 钟mh | 来源:发表于2019-01-31 16:54 被阅读0次

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 可以捕获异常。

相关文章

网友评论

      本文标题:thread UncaughtExceptionHandler

      本文链接:https://www.haomeiwen.com/subject/xtclsqtx.html