Java多线程问题--线程运行时出现异常的解决办法

private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(32, 128, 120, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(512),
r -> {
Thread t = new Thread(r);
t.setUncaughtExceptionHandler((th, e) ->
logger.error(LogBuilder.log("ThreadPoolExecutor", "uncaught exception," + th.getName()), e)
);
t.setName("th" + t.getId());
return t;
},
(r, exec) -> {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}

exec.getQueue().add(r);
});

posted on 2019-12-29 19:28  二云  阅读(991)  评论(0编辑  收藏  举报

导航