Program does not terminate immediately when all ExecutorService tasks are done

后端 未结 6 1103
一整个雨季
一整个雨季 2020-12-29 21:22

I put a bunch of runnable objects into an ExecutorService:

// simplified content of main method
ExecutorService threadPool = Executors.newCachedThreadPool();         


        
6条回答
  •  温柔的废话
    2020-12-29 21:47

    Basically on an ExecutorService you call shutdown() and then awaitTermination():

    ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
    while(...) {
       taskExecutor.execute(new MyTask());
    }
    taskExecutor.shutdown();
    try {
      taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
      ...
    }
    

提交回复
热议问题