to shutdown gracefully the Executor Service you need to proceed as following
- executorService.shutdownNow();
- executorService.awaitTermination();
1 the executor will try to interrupt the threads that it manages, and refuses all new tasks from being submitted.
- Wait a while for existing tasks to terminate
below an example of graceful Executor shutdown
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
please find here a complete detailed anwser
hope help