shutdown and awaitTermination which first call have any difference?

shutdown means the executor service takes no more incoming tasks.

awaitTermination is invoked after a shutdown request.

You need to first shut down the service and then block and wait for threads to finish.

If you want to see all threads finish running and insist on using awaiTermination, you need to set the timeout parameter to be big enough. So you could do:

eService.shutdown();
if (!eService.awaitTermination(60000, TimeUnit.SECONDS))
    System.err.println("Threads didn't finish in 60000 seconds!");
}

Alternatively, you could do:

eService.shutdown();
while (!eService.isTerminated()) {

}

This way you are able to ensure all threads are finished running unless they are interrupted unexpectedly.


Reading the documentation always helps:

shutdownNow :

Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. These tasks are drained (removed) from the task queue upon return from this method.

This method does not wait for actively executing tasks to terminate. Use awaitTermination to do that.

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. This implementation cancels tasks via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate

shutdown:

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.

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

awaitTermination:

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.


You should call shutdown first. Otherwise, you might be waiting for a very long time, since awaitTermination doesn't actually shut down your executor.

If you wanted to wait for tasks to complete, rather than wait for the executor to shut down, then you should use invokeAll.