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();
? }
}
添加回答
舉報