Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to shutdown an ExecutorService for a webapplication running in a Tomcat container?

Is it necessary to shutdown an ExecutorService at some point if it runs in a Tomcat container for a Servlet. If yes, then where should I call the shutdown? I tried adding it after the submit() call but when I fire another request to the Servlet from a client browser, I get a RejectedExecutionException which is probably because I did a shutdown? I am trying to understand how it works in a Servlet within Tomcat and how I should use it.

I am doing the following in my webApplication (which seems to work fine without any shutdown):

// In configuration class
@Bean (name = "executorService")
public ExecutorService executorService() {
  return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() + 1);
}

// In some other class
@Qualifier("executorService")
@Autowired
private ExecutorService executorService;
....
private void load() {
  executorService.submit(new Runnable() {
    @Override
    public void run() {
        doSomethingInTheBackground();
    }
});

// If I enable this I will get a RejectedExecutionException 
// for a next request.
// executorService.shutdown();

}

like image 911
Diyarbakir Avatar asked Jul 29 '15 19:07

Diyarbakir


People also ask

Do you need to shutdown down ExecutorService?

ExecutorService must be shutdown explicitly to reclaim the resources (CPU & Memory) occupied by threads which have already finished their job but still exist.

What happens if we don't shut down ExecutorService?

Using shutdown() and awaitTermination​() In general, the ExecutorService will not be automatically destroyed when there is no task to process. It will stay alive and wait for new tasks to come. It simply means that JVM will not terminate if we are expecting it to.

How do you shut down an ExecutorService?

To properly shut down an ExecutorService, we have the shutdown() and shutdownNow() APIs. The shutdown() method doesn't cause immediate destruction of the ExecutorService. It will make the ExecutorService stop accepting new tasks and shut down after all running threads finish their current work: executorService.

Where is ExecutorService shut down?

Two different methods are provided for shutting down an ExecutorService. The shutdown() method will allow previously submitted tasks to execute before terminating, while the shutdownNow() method prevents waiting tasks from starting and attempts to stop currently executing tasks.


1 Answers

The idea behind the ExecutorService is to reuse threads. Creating threads is expensive and usually it is more efficient to create a thread once and then use that same thread multiple times. This is exactly what an ExecutorService does: it manages a pool of (possibly idle) threads and assigns work to them when you call its submit methods.

In a typical application you therefore do not want to shutdown the ExecutorService. You should however shut the ExecutorService down properly if your application is terminated. Since you are using Spring you don't have to worry about that:

By default, beans defined using Java config that have a public close or shutdown method are automatically enlisted with a destruction callback. [See the documentation.]

That means, if you close the ApplicationContext, Spring will automatically shutdown the ExecutorService for you.

like image 88
hzpz Avatar answered Oct 15 '22 05:10

hzpz