2

I was checking the javadoc of the ExecutorService#shutDown() method and it states:

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

I understand "are executed" as: it waits until previously submitted tasks finish their execution. But it also states:

This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

which seems to say that it cancels the tasks that have already been submitted.

Could somebody clarify what happens when calling shutdown and some previously submitted tasks have not finished their execution?

EDIT
I use an executor to launch a few tasks, but after that I don't need the executor any longer so I want to let it know that it can release the threads when the tasks are complete.

assylias
  • 321,522
  • 82
  • 660
  • 783

1 Answers1

7

The point is that the shutDown method returns without waiting for the previously submitted tasks to complete, but it still lets them complete. You might want to think of it as a "start shutting down" method.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Does it mean queued tasks will be not be taken for execution after shutdown? Suppose t1, t2 ,t3 are submitted. If shutdown is initiated when t2 is executing, t1 executed and t3 is yet to execute. I expect t3 to execute because **previously submitted tasks are executed**, am I wrong – Nilesh Feb 01 '17 at 09:30
  • @Nilesh: Yes, that would be executed. But the `shutDown` method won't wait for `t3` to be executed before returning. – Jon Skeet Feb 01 '17 at 09:34