Reputation: 36
To avoid a prolonged wait time, it is recommended that you invoke a shutdown()
prior to invoking awaitTermination()
, since latter isn't for killing the executor. However, for waiting task completion, should you instead be utilizing invokeAll()
in lieu of waiting for executor shutdown?
The javadoc indicates that if you wish to cause interruption, one should do shutDownNow()
.
So does shutdown()
actually interrupt already executing tasks, as in is it graceful and orderly?
Thanks!
Upvotes: 0
Views: 661
Reputation: 238
Think of an ExecutorService as a long-lived object, something that's been around for a while, processing lots of work, not just the 3 things you need done right now.
.invokeAll
is asking: "Do my 3 things please, when you have the time of course"
.shutdown
is like saying: "Your shift has ended. Finish what you're doing and go home. (And don't take any new work, even if someone else asks you to)"
.awaitTermination
is not telling it to stop, it's just like standing over its shoulder waiting for it to stop eventually: "I'm going to just stand here and wait until you go home, mmm kay?"
.shutdownNow
is like handing the executor a knife and telling it to go off and murder-death-kill all of its internal workers. It will tell you about any backlog of work that hadn't even been started yet before slipping out to do it's murdering. But there's no guarantee that it will actually be successful at killing anything.
Upvotes: 3
Reputation: 7546
shutdown documentation says:
/** 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.
*
* <p>This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
shutdownNow documentation says:
/**
* Attempts to stop all actively executing tasks, halts the
* processing of waiting tasks, and returns a list of the tasks
* that were awaiting execution.
*
* <p>This method does not wait for actively executing tasks to
* terminate. Use {@link #awaitTermination awaitTermination} to
* do that.
*
* <p>There are no guarantees beyond best-effort attempts to stop
* processing actively executing tasks. For example, typical
* implementations will cancel via {@link Thread#interrupt}, so any
* task that fails to respond to interrupts may never terminate.
*
Invoke all documentation says:
/**
* Executes the given tasks, returning a list of Futures holding
* their status and results when all complete.
* {@link Future#isDone} is {@code true} for each
* element of the returned list.
* Note that a <em>completed</em> task could have
* terminated either normally or by throwing an exception.
* The results of this method are undefined if the given
* collection is modified while this operation is in progress.
*/
invokeAll is similar to execute but receives a collection of tasks.
These documentation comments for both method should clearly make you understand the difference.
Upvotes: 5