How to wait for a ThreadPoolExecutor to finish

后端 未结 6 1712
渐次进展
渐次进展 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 18:51

    Here's a variant on the accepted answer that handles retries if/when InterruptedException is thrown:

    executor.shutdown();
    
    boolean isWait = true;
    
    while (isWait)
    {
        try
        {             
            isWait = !executor.awaitTermination(10, TimeUnit.SECONDS);
            if (isWait)
            {
                log.info("Awaiting completion of bulk callback threads.");
            }
        } catch (InterruptedException e) {
            log.debug("Interruped while awaiting completion of callback threads - trying again...");
        }
    }
    

提交回复
热议问题