简体   繁体   English

如何在停止tomcat服务器时检查线程是否被终止或继续运行?

[英]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),它们也将非常有用。

Explanation here 这里解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tomcat停止时如何停止执行程序线程? - How to stop an executor thread when Tomcat is stopped? Eclipse + Tomcat如何让服务器保持运行 - Eclipse + Tomcat how to keep the server running Android如何在用户杀死应用程序后继续运行后台服务? - Android How to keep Running the background service when app is killed by user? 如何知道什么线程在 tomcat 中被杀死了? - How to know what got a thread killed in tomcat? 即使在服务被系统杀死后,Service 内部的线程也会继续运行 - Thread inside Service keep running even after the service is killed by the system 如何停止线程,以使在其上运行的任务永久停止? - How can you stop a thread so that the task running on it stops permanentl? 如果出现错误,如何检查Tomcat Web应用程序是否正在运行并重定向到另一台服务器? - How to check if Tomcat Web Application is running and redirect to another server if there is an error? 应用程序被杀死时如何停止接收通知? - How to stop receiving notifications when app is killed? 如何检查线程是否正在运行 - how to check thread is running 如何在JVM被杀死之前使线程永远存活? - How to keep a thread alive forever until JVM is killed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM