The difference between turning off and on.

I want to know the main difference between shutdown() and shutdownNow() to close the Executor Service ? As far as I understand, shutdown() should be used for graceful shutdown, which means all tasks that are started and queued for processing, but not started, must be completed, and shutdownNow() ends abruptly , which means that some incomplete tasks are canceled, and also canceled tasks are also canceled. Is there anything else that is implicit / explicit that I am missing?

PS: I found one more question in https://stackoverflow.com/a/166269/2126/ but not quite what I want to know.

+44
java java.util.concurrent
Jul 17 2018-12-17T00:
source share
3 answers

So you can think of it this way:

  • shutdown() will simply tell the executing service that it cannot accept new tasks, but already submitted tasks continue to run
  • shutdownNow() will do the same And will try to cancel already submitted tasks by interrupting the corresponding threads. Note that if your tasks ignore interruption, shutdownNow will behave exactly like shutdown .

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

  • with shutdown , the output is Still waiting after 100ms: calling System.exit(0)... because the task in progress is not interrupted and continues to work.
  • with shutdownNow , the output is interrupted and Exiting normally... because the task in Exiting normally... is interrupted, interrupts the interrupt and stops what it is doing (aborts the while loop).
  • with shutdownNow , if you comment out lines in a while loop, you will get Still waiting after 100ms: calling System.exit(0)... because the interrupt is no longer handled by the running task.
 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..."); } 
+84
Jul 17 '12 at 10:05
source share

From javadocs :

void shutdown

Initiates an orderly shutdown in which previously submitted tasks but no new tasks will be accepted.

List<Runnable> shutdownNow()

Attempting to stop all actively executing tasks stops the processing of waiting and returns a list of expected tasks to be completed.

There are no guarantees other than trying to maximize the effort to stop actively processing tasks.

For example, typical implementations will be canceled through Thread.interrupt (), so any task that does not respond to interrupts will never stop.

Returns: a list of tasks that never started

+2
Jul 17 2018-12-12T00:
source share
  • shutdown() :

To terminate threads inside an ExecutorService, you call its shutdown() method. The ExecutorService will not be closed immediately, but it will no longer accept new tasks, and as soon as all threads complete the current tasks, the ExecutorService will shut down. All tasks passed to the ExecutorService before shutdown () are executed.

  • shutdownNow() :

If you want to close the ExecutorService service immediately, you can call the shutdownNow() method. This will try to immediately stop all tasks being performed and skip all submitted but not processed tasks. There is no data on the completion of tasks. Perhaps they stop, perhaps the execution to the end. This is the best attempt at effort.

+2
Sep 15 '15 at 10:00
source share



All Articles