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
- Will not accept new task
- 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