Waiting for all the threads to finish before shutting down the Executors

后端 未结 3 637
执念已碎
执念已碎 2020-12-18 12:05

Here is my code snippet.

ExecutorService executor = Executors.newFixedThreadPool(ThreadPoolSize);
while(conditionTrue)
{
ClassImplementingRunnable c = new Cl         


        
3条回答
  •  悲&欢浪女
    2020-12-18 12:46

    You generally use the following idiom:

    executor.shutdown();
    executor.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
    
    • shutdown just says that the executor won't accept new jobs.
    • awaitTermination waits until all the tasks that have already been submitted finish what they are doing (or until the timeout is reached - which won't happen with Integer.MAX_VALUE - you might want to use a lower value).

提交回复
热议问题