How to wait for a ThreadPoolExecutor to finish

后端 未结 6 1724
渐次进展
渐次进展 2020-12-13 18:15

My Question: How to execute a bunch of threaded objects on a ThreadPoolExecutor and wait for them all to finish before moving on?

I\'m new to Thread

6条回答
  •  被撕碎了的回忆
    2020-12-13 19:11

    You should loop on awaitTermination

    ExecutorService threads;
    // ...
    // Tell threads to finish off.
    threads.shutdown();
    // Wait for everything to finish.
    while (!threads.awaitTermination(10, TimeUnit.SECONDS)) {
      log.info("Awaiting completion of threads.");
    }
    

提交回复
热议问题