Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between shutdown and shutdownNow of Executor Service

I want to know the basic difference between shutdown() and shutdownNow() for shutting down the Executor Service?

As far as I understood:

shutdown() should be used for graceful shutdown which means all tasks that were running and queued for processing but not started should be allowed to complete

shutdownNow() does an abrupt shut down meaning that some unfinished tasks are cancelled and unstarted tasks are also cancelled. Is there anything else which is implicit/explicit that I am missing?

P.S: I found another question on How to shutdown an executor service related to this but not exactly what I want to know.

like image 685
Geek Avatar asked Jul 17 '12 10:07

Geek


People also ask

What is executor shutdown?

Shutting down the ExecutorService shutdown() - when shutdown() method is called on an executor service, it stops accepting new tasks, waits for previously submitted tasks to execute, and then terminates the executor. shutdownNow() - this method interrupts the running task and shuts down the executor immediately.

Should executor services be shut down?

An ExecutorService should be shut down once it is no longer needed to free up system resources and to allow graceful application shutdown. Because the threads in an ExecutorService may be nondaemon threads, they may prevent normal application termination.

Does executor service shutdown automatically?

Using shutdown() and awaitTermination​() In general, the ExecutorService will not be automatically destroyed when there is no task to process. It will stay alive and wait for new tasks to come.

How do you stop an executor service?

To properly shut down an ExecutorService, we have the shutdown() and shutdownNow() APIs. The shutdown() method doesn't cause immediate destruction of the ExecutorService. It will make the ExecutorService stop accepting new tasks and shut down after all running threads finish their current work: executorService.


1 Answers

In summary, you can think of it that way:

  • shutdown() will just tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run
  • shutdownNow() will do the same AND will try to cancel the already submitted tasks by interrupting the relevant threads. Note that if your tasks ignore the interruption, shutdownNow will behave exactly the same way as shutdown.

You can try the example below and replace shutdown by shutdownNow to better understand the different paths of execution:

  • with shutdown, the output is Still waiting after 100ms: calling System.exit(0)... because the running task is not interrupted and continues to run.
  • with shutdownNow, the output is interrupted and Exiting normally... because the running task is interrupted, catches the interruption and then stops what it is doing (breaks the while loop).
  • with shutdownNow, if you comment out the lines within the while loop, you will get Still waiting after 100ms: calling System.exit(0)... because the interruption is not handled by the running task any longer.
public static void main(String[] args) throws InterruptedException {     ExecutorService executor = Executors.newFixedThreadPool(1);     executor.submit(new Runnable() {          @Override         public void run() {             while (true) {                 if (Thread.currentThread().isInterrupted()) {                     System.out.println("interrupted");                     break;                 }             }         }     });      executor.shutdown();     if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {         System.out.println("Still waiting after 100ms: calling System.exit(0)...");         System.exit(0);     }     System.out.println("Exiting normally..."); } 
like image 162
assylias Avatar answered Sep 21 '22 08:09

assylias