2 回答

TA贡献1934条经验 获得超2个赞
ExecutorService.shutdown() 不会等待其所拥有的任务终止,它只会停止接受新任务。
调用 shutdown 后,如果您想等待执行程序服务完成,则应该在执行程序服务上调用 waitTermination。
因此,当您开始将图像写入文件时,所有任务尚未完成执行。

TA贡献2021条经验 获得超8个赞
要添加答案,您可以使用以下代码来关闭线程池
以下方法分两个阶段关闭 ExecutorService,首先调用 shutdown 拒绝传入任务,然后调用 shutdownNow(如有必要)取消任何延迟任务:
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
添加回答
举报