Is an executor object meant to be reused after a shutdown
? I mean if I call shutdown
or shutdownNow
after the executor is terminated, should I do new
to create a new thread pool or is it possible to somehow "reset"/reuse the previously terminated executor and reuse it?
Update:
If I need to create new thread pool, how can I "understand" that the previous has been stopped?
E.g. The following:
public void startPool(){ if(threadPool != null && !threadPool.isShutdown()){ return; } threadPool = Executors.newCachedThreadPool(); //other stuff } public void stopPool(){ if(threadPool != null){ threadPool.shutdown(); } }
Will not work. If I call stop
and then start
a new thread pool will not be created due to conditions. What is the proper way to code this?
From the doc:
Upon termination, an executor has no tasks actively executing, no tasks awaiting execution, and no new tasks can be submitted
so (briefly), no, you can't reuse it. From the shutdown() doc:
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
The methods isTerminated()
and isShutdown()
can be used to determine if the executor has been shutdown, and that the tasks have completed respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With