Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an executor meant to be reused?

Tags:

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?

like image 464
Jim Avatar asked Feb 20 '13 10:02

Jim


1 Answers

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.

like image 88
Brian Agnew Avatar answered Mar 19 '23 23:03

Brian Agnew