As we know, the java.util.concurrent.Executors
contains many methods such as
newCachedThreadPool
newFixedThreadPool
newScheduledThreadPool
newSingleThreadExecutor
newSingleThreadScheduledExecutor
They return
ExecutorService
, which contains theexecute(Runnable task)
method. However, when calling theexecute(Runnable task)
ofExecutorService
returned from the aforementioned factory methods, it could only terminate by callingshutdown()
orshutdownNow()
For instance, if we add the following code to the main
method,
ExecutorService e = Executors.newSingleThreadExecutor();
e.execute(() -> system.out.println("test"));
the calling the the main program will never terminate as the shutdown()
or shutdownNow()
is not called. So a program containing the following snippet in main will terminate
ExecutorService e = Executors.newSingleThreadExecutor();
e.execute(() -> system.out.println("test"));
e.shutdown();
However, some subclasses of ExecutorService
such as the one returned by calling Executors.newWorkStealingPool
or the ForkJoinPool
can terminate without calling shutdown()
or shutdownNow()
So my QUESTION is: why does the execute()
of the ExecutorService
returned from the aforementioned factory methods starting with "new" not terminate without calling shutdown()
or shutdownNow()
from the design point of view?