Search code examples
javaconcurrencyjava.util.concurrent

ExecutorService shutdown


I am confused about the javadoc of ExecutorService#shutdown method. Aren't these contradictory statements?

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

If it can orderly shutdown previously submitted tasks, then how can't it wait for them to complete execution?


Solution

  • It means that the method returns immediately in the thread that you call it in, but tasks that haven't yet been executed might still be running, in other threads.

    If you want your program to wait until the tasks that had been submitted previously have finished, you have to call awaitTermination after calling shutdown.