Erik
Erik

Reputation: 75

Do I have to care about the shutdown of a ThreadPoolExecutor?

I have a REST application (stack: Java8/Tomcat7/Spring4.2/Jersey2.15) and use in my REST endpoint a thread pool (ThreadPoolExecutor).

      private ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);

The endpoint class is configured with the spring annotation @Component (so it is by default a singleton).

In the javadoc of ThreadPoolExecutor I read this:

Finalization A pool that is no longer referenced in a program AND has no remaining threads will be shutdown automatically. If you would like to ensure that unreferenced pools are reclaimed even if users forget to call shutdown(), then you must arrange that unused threads eventually die, by setting appropriate keep-alive times, using a lower bound of zero core threads and/or setting allowCoreThreadTimeOut(boolean).

So when I do not call threadPool.shutdown() the pool will be shutdown when the application ends (normally in this case when the tomcat is shutdown or the application will be stopped).

My question is: Do I have to care about the shutdown of the pool? Can side effects occure? (e.g. mem leaks, long Tomcat shutdown time,...) Or is it rock solid?

Upvotes: 3

Views: 510

Answers (1)

gkatzioura
gkatzioura

Reputation: 2820

It is generally a good practice to close the thread pool gracefully. It might take more time to shutdown your tomcat instance (in case of unfinished jobs), but the reward is a more managed and safe application and might save you from future trouble shooting in case of unfinished jobs.

Since you already use spring you might want to take a look to a managed spring thread pool by using the ThreadPoolTaskExecutor.

Upvotes: 4

Related Questions