[英]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. 我有一个10个线程的线程池来处理某些任务,它们在我的tomcat服务器中的web应用程序中继续运行。 I am using the ExecutorService to create Thread Pool as given below.
我使用ExecutorService创建线程池,如下所示。
ExecutorService executor = Executors.newFixedThreadPool(10);
Will these thread be killed or keep running after stopping the tomcat server without executing executor.shutdown()? 在不执行executor.shutdown()的情况下停止tomcat服务器后,这些线程会被杀死还是继续运行?
Thanks. 谢谢。
As others have mentioned if you execute tomcat shutdown, it's going to terminate your thread. 正如其他人提到的,如果执行tomcat shutdown,它将终止你的线程。
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.
所以我认为你需要在ServletContextListener中实现你的关闭逻辑并使用contextDestroyed方法。
When the Tomcat shuts down, all remaining threads will be terminated as well. 当Tomcat关闭时,所有剩余的线程也将被终止。 Your threadpool might stop tomcat from shutting down gracefully.
您的线程池可能会阻止tomcat正常关闭。 Eventually, the process might be killed by the script that runs your Tomcat (so, it depends on how you initiate/shutdown your Tomcat).
最终,该进程可能会被运行Tomcat的脚本杀死(因此,这取决于您启动/关闭Tomcat的方式)。
Creating threads inside a web container not always recommended because they do not let container shuts down gracefully. 不总是建议在Web容器内创建线程,因为它们不会让容器正常关闭。 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.
您应该使用托管线程池,如果您想访问其他容器的托管资源(例如EJB),它们也将非常有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.