0

The following is the javadoc of ExecutorService#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.
 *
 * <p>This method does not wait for previously submitted tasks to
 * complete execution.  Use {@link #awaitTermination awaitTermination}
 * to do that.
 *
 * @throws SecurityException if a security manager exists and
 *         shutting down this ExecutorService may manipulate
 *         threads that the caller is not permitted to modify
 *         because it does not hold {@link
 *         java.lang.RuntimePermission}{@code ("modifyThread")},
 *         or the security manager's {@code checkAccess} method
 *         denies access.
 */
void shutdown();

It says

  1. Will not accept new task
  2. Will NOT wait for submitted task to complete

I have no doubt about the first point, but I have thought that the executor will wait submitted task to complete, like the following code:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService es = Executors.newFixedThreadPool(1);
        es.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("starts to run 1");
                try {
                    Thread.sleep(10 * 1000);
                    System.out.println("end to run 1");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        es.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("starts to run 2");
                try {
                    Thread.sleep(10 * 1000);
                    System.out.println("end to run 2");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread.sleep(1 * 1000);
        es.shutdown();

        System.out.println("Job is done");


    }


}

The two tasks are submitted,since there is only one thread in the pool, so there is one task running while the other is in the queue waiting for schedule.

But, both tasks are eventually run.

So, I would ask what the javadoc mean by This method does not wait for previously submitted tasks to complete execution

Tom
  • 5,848
  • 12
  • 44
  • 104
  • 1
    It means that "Job is done" is printed before both submitted tasks are finished. – daniu Aug 31 '18 at 09:40
  • 3
    The documentation says: *Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted*. The sentence you reported means just that once `shutdown` is called the execution goes on without waiting. If you want to shutdown everything without completing active tasks, call `shutdownNow`. – BackSlash Aug 31 '18 at 09:41

1 Answers1

3

It means that the shutdown method returns immediately. It does not wait for scheduled and already running tasks to complete before returning to its caller. This means that the ExecutorService can still take some time to clean up and terminate itself (which it will eventually do, after all running tasks have completed).

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • But, As my test case shows, the two tasks both got completed. – Tom Aug 31 '18 at 09:42
  • 1
    Yes, they got completed, but the `shutdown` method returned before they got completed. It did not wait. – Thilo Aug 31 '18 at 09:43
  • 4
    @Tom Where did you read that the tasks will not be completed? That statement means just that `shutdown` is a non-blocking call, it will not block until the tasks are completed to return. This doesn't mean that tasks won't complete. – BackSlash Aug 31 '18 at 09:44
  • 1
    Contrast this with `shutdownNow`, which cancels all scheduled tasks. – Thilo Aug 31 '18 at 09:44
  • 1
    OK,thanks,That's to say, the submitted tasks will be run before the pool really shutdown. – Tom Aug 31 '18 at 09:45
  • Thanks @Thilo very much – Tom Aug 31 '18 at 09:47