比方说下面这个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;
}
}
}
这两部分是同步的啊,怎么会出现你说的那种情况?
当然如果在
shutdown()
之后执行添加任务,会失败的。