Reputation: 2540
According to documentation, when shutdown()
is invoked, any tasks that were already submitted (I assume via submit()
or execute
) will be executed. When shutdownNow()
is invoked, the executor will halt all tasks waiting to be processed, as well as attempt to stop actively executing tasks.
What I would like to clarify is the exact meaning of "waiting to be processed." For example, say I have an executor, and I call execute()
on some number of Runnable
objects (assume all of these objects effectively ignore interruptions). I know that if I now call shutdown
, all of these objects will finish executing, regardless.
However, if I call shutdownNow
at this point, will it have the same effect as calling shutdown
? Or are some of the objects not executed? In other words, if I want an executor to exit as fast as possible, is my best option always to call shutdownNow()
, even when the Runnables
passed to the executor all effectively ignore interruptions?
Upvotes: 2
Views: 8016
Reputation: 719376
What I would like to clarify is the exact meaning of "waiting to be processed".
It means all tasks whose run()
method has not yet been called (by the executor).
If I call shutdownNow at this point, will it have the same effect as calling shutdown?
No.
Or is it possible that some of the objects will not be executed?
That is correct.
In other words, if I want an executor to exit as fast as possible, is my best option always to call shutdownNow(), even when the Runnables passed to the executor all effectively ignore interruptions?
That is correct.
Better still, recode the Runnables to pay attention to interrupts ... or put a timeout on the shutdown ...
Upvotes: 1
Reputation: 63955
Let's say you have this fabulous Runnable
that is not interruptible for 10 seconds once it's started:
Runnable r = new Runnable() {
@Override
public void run() {
long endAt = System.currentTimeMillis() + 10000;
while (System.currentTimeMillis() < endAt);
}
};
And you have an executor with just 1 thread and you schedule the runnable 10 times:
ExecutorService executor = Executors.newFixedThreadPool(1);
for (int i = 0; i < 10; i++)
executor.execute(r);
And now you decide to call shutdown
:
shutdown
can be used if you want a "short lived" executor just for a few tasks. You can immediately call shutdown
and it will get cleaned up later.Alternatively shutdownNow()
:
Upvotes: 2
Reputation: 12484
The API for shutdownNow
method says that :
There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
Upvotes: 0