Barnwal Vivek
Barnwal Vivek

Reputation: 169

How to check if thread is killed or keep running when you stop the tomcat Server?

I have a thread pool of 10 threads to serve some task and they are keep running in my web application in tomcat server. I am using the ExecutorService to create Thread Pool as given below.

ExecutorService executor = Executors.newFixedThreadPool(10);

Will these thread be killed or keep running after stopping the tomcat server without executing executor.shutdown()?

Thanks.

Upvotes: 3

Views: 2919

Answers (3)

Sas
Sas

Reputation: 2503

As others have mentioned if you execute tomcat shutdown, it's going to terminate your thread.

However, as per your comment you are trying to gracefully kill your threads. So I think you need to implement your shutdown logic in the ServletContextListener and use the contextDestroyed method.

Upvotes: 1

PyThon
PyThon

Reputation: 1067

Creating threads inside a web container not always recommended because they do not let container shuts down gracefully. You should use managed thread pool, they will also be useful if you want to access other container's managed resources for example EJB's.

Explanation here

Upvotes: 0

Boon
Boon

Reputation: 2151

When the Tomcat shuts down, all remaining threads will be terminated as well. Your threadpool might stop tomcat from shutting down gracefully. Eventually, the process might be killed by the script that runs your Tomcat (so, it depends on how you initiate/shutdown your Tomcat).

Upvotes: 0

Related Questions