3

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.

Barnwal Vivek
  • 151
  • 1
  • 11

3 Answers3

1

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.

Sas
  • 2,313
  • 5
  • 27
  • 46
0

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).

Boon
  • 869
  • 11
  • 24
  • 'it depends on how you initiate/shutdown your Tomcat' Could you please explain a little more? I am shutting down it by using shutdown.bat file of Apache-tomcat-6.0.44\bin. – Barnwal Vivek Mar 18 '16 at 08:11
  • what is your current experience? when you run shutdown.bat... does it hang there? – Boon Mar 18 '16 at 08:14
  • It does not hangs but how to be sure that these threads are killed gracefully? – Barnwal Vivek Mar 18 '16 at 08:18
0

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

Community
  • 1
  • 1
PyThon
  • 957
  • 7
  • 22