Is an executor meant to be reused?

Jim picture Jim · Feb 20, 2013 · Viewed 8.3k times · Source

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?

Answer

Brian Agnew picture Brian Agnew · Feb 20, 2013

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.