Java ExecutorService when to call shutdown()

I’m new to Java multithreading. I’m using ExecutorService to download a bunch of web pages to analyze. Currently my approach is to submit all Callables and put the Future objects in a list in a loop, then call shutdown() and awaitTermination(), finally I process the future list.
My question is in my case, is it OK to put shutdown() after filling up the future list like this? As get() of Future already blocks until the task is done, seems that I should instead call it when I finish processing all the futures.

calling ExecutorService.shutDown() in java

i am starting to learn the ExecutorService class. The documentation (and tutorials online) say to always call ExecutorService.shutDown() to reclaim resources. however, the documentation also says that

Shutdown executor without executorService in Java

Is there a way to shutdown an Executor in Java without first casting it to ExecutorService? Basically i have a class called Exx that implements Executor. I want to shutdown Exx. How would I do that? E

Java shutdown hooks with ExecutorService

I’m running into a problem when shutting down my application that the ExecutorService has been terminated…What’s a good way to deal with this? public class TradingLock { private ExecutorService exec

ExecutorService with Task chain shutdown

I have an ExecutorService which executes tasks which itself submit new tasks to the ExecutorService. When I call .shutdown(), I want the running tasks still being able to submit new tasks to the Execu

Determining shutdown time of ExecutorService

I have an ExecutorService that processes some tasks. The client threads can call shutdown() on the ExecutorService. I want to run some cleanup code after the ExecutorService has completely shutdown. I

ExecutorService shutdown

I am confused about the javadoc of ExecutorService#shutdown method. Aren’t these contradictory statements? Initiates an orderly shutdown in which previously submitted tasks are executed, but no new t

Android, Java: ExecutorService execute after shutdown

I have a ExecutorService: private final ExecutorService listenDataExecutorService = Executors.newSingleThreadExecutor(); In same case I need executor to stop doing his job. For this I call listenData

Java – How to make an ExecutorService running inside another ExecutorService not to shutdown when shutdown is invoked on the outer ExecutorService?

I have a Executor service running inside another executor service to send emails. If I call shutdown on the outer executor, it is waiting for the inner executor service to shutdown which is drasticall

When to shutdown executorservice in android application

Lets assume I’m planning to use one executorservice in my entire application, to which I’m sending new runnable’s or callable’s to execute/submit, and I ommit to shutdown right after I do that. I just

Shutdown ExecutorService gracefully when main-thread completes

In a utility library i am creating a ExecutorService ExecutorService es = Executors.newSingleThreadExecutor(); The main thread will then post some tasks to this ExecutorService. When the main-thread


Note the javadoc of 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.

This is a clean stop. The submitted tasks will run until they are done. Since you also have awaitTermination() which

Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.

Your Future#get() method calls will return immediately (if the awaitTermination() didn’t time out). There is no issue with what you are doing.

Make sure your tasks don’t run forever.

There are two methods

1. shutdown()

as @Sotirios said, thread will completes the current tasks but did not add any new task for execution. So the current tasks will run to finish

2. shutdownNow()

It suddenly terminates all the tasks.

So by using the shutdown() method you can check weather all tasks are finished or not and can wait until all tasks are finished. To acheive what you want, you can use isTerminated() method in the infinite loop as


So when all the finished next sequential action will be taken.

There is no point using a thread pool if you shut it down after you’ve used it. It’s designed to allow you to reuse the threads.

Instead, put the the Executor in a static field (or better, inject it with a DI framework like Spring). Then use java.util.concurrent.ExecutorService.invokeAll(Collection<? extends Callable<T>>), which blocks until all the tasks you submitted have completed.