比方说下面这个demo
这个方法在创建线程池threadPoolExecutor后,通过for循环向线程池加入任务的过程中,shutdown()方法是不是会先执行,导致任务无法全部加入到线程池中就把线程池关闭了
public String demo(List<File> file,String path) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
for (int x = 0; x < file.size(); x++) {
final int temp = x;
threadPoolExecutor.execute(() -> {
logger.info("threadName:" + Thread.currentThread().getName());
//添加任务temp
});
}
threadPoolExecutor.shutdown();
logger.info("执行shutdown():线程池关闭");
while (true) {
if (threadPoolExecutor.isTerminated()) {
logger.info("线程池所有的子线程全部执行完毕!");
return path;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…