ExecutorService, how to wait for all tasks to finish

前端 未结 15 2221
攒了一身酷
攒了一身酷 2020-11-22 15:45

What is the simplest way to to wait for all tasks of ExecutorService to finish? My task is primarily computational, so I just want to run a large number of jobs

15条回答
  •  野性不改
    2020-11-22 16:45

    I will just wait for the executor to terminate with a specified timeout that you think it is suitable for the tasks to complete.

     try {  
             //do stuff here 
             exe.execute(thread);
        } finally {
            exe.shutdown();
        }
        boolean result = exe.awaitTermination(4, TimeUnit.HOURS);
        if (!result)
    
        {
            LOGGER.error("It took more than 4 hour for the executor to stop, this shouldn't be the normal behaviour.");
        }
    

提交回复
热议问题