What is the difference between
ExecutorService eService = Executors.newFixedThreadPool(2);
eService.execute(new TestThread6());
eService.execute(new TestThre
Main Difference
shutdown()-
1. Doesn't block the calling a thread i.e. the thread who called the shutdown().
2. Excecutor doesn't accept any new task after calling shutdown().
awaitTermination() -
1. Blocks the calling thread. (as join() method do)
Point of Confusion:- If shutdown() does not kill the previously submitted tasks, why do we need awaitTermination()?
awaitTermination means waiting for the tasks to be completed/terminated, right? shutdown()
is doing the same- waiting for any task which is already submitted along with the running tasks to continue till completed/terminated, then why another method awaitTermination(..)
? Below is the explanation:
Suppose you can wait for 10 min only to complete all the task that are submitted and after that want to call shutdownNow()
(---you already know what it do) then use awaitTermination(long timeout, TimeUnit unit)
after calling shutdown().
Notice the method arguments in awaitTermination(long timeout, TimeUnit unit)
. This timeout is the key here.
If there is no time restriction, shutdown() is OK. No need of awaitTermination().