How to wait for all threads to finish, using ExecutorService?

前端 未结 26 2041
你的背包
你的背包 2020-11-22 01:55

I need to execute some amount of tasks 4 at a time, something like this:

ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
    tas         


        
26条回答
  •  再見小時候
    2020-11-22 02:32

    Clean way with ExecutorService

     List> results = null;
     try {
         List> tasks = new ArrayList<>();
         ExecutorService executorService = Executors.newFixedThreadPool(4);
         results = executorService.invokeAll(tasks);
     } catch (InterruptedException ex) {
         ...
     } catch (Exception ex) {
         ...
     }
    

提交回复
热议问题