今天在工作的时候,遇见了一个线程阻塞的问题,然后定位代码,找出了问题的所在:
这是我们线程池的初始化代码:
RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardPolicy();
executor = new ThreadPoolExecutor(5, 20, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(5000),r -> new Thread(r, "xxx-" + r.hashCode()), handler);
在任务中我们的调用方式:
CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
for (Task task: taskList) {
executor.execute(new Task001(countDownLatch, task));