[英]How to stop an executor thread when Tomcat is stopped?
I am using an executor service by creating a fixed number of threads to do a HTTP GET data retrieval. 我通过创建固定数量的线程来执行HTTP GET数据检索来使用执行程序服务。
executorService = ExecutorServiceFactory.getInstance().getExecutorService(Config.getInstance().getNumberOfThreads(), "ThreadExecutor_"+UIDFactory.getInstance().createSessionID());
executorService.execute(new Retrieve(data));
private class Retrieve implements Runnable{
private Vector<String> data;
public WADORetrieve(Vector<String> data) {
this.data = data;
}
@Override
public void run() {
for(int i=0;i<data.length;i++){
fetch(data[i]);
}
}
}
When Tomcat is stopped we get this error: 当Tomcat停止时,我们得到以下错误:
SEVERE: The web application [/viewer] appears to have started a thread named [ThreadExecutor_5161616156] but has failed to stop it. 严重:Web应用程序[/ viewer]似乎已启动名为[ThreadExecutor_5161616156]的线程,但未能停止该线程。 This is very likely to create a memory leak. 这很可能造成内存泄漏。
Is this a real issue? 这是一个真正的问题吗? What can I do to stop tomcat properly without these service errors. 在没有这些服务错误的情况下,我该怎么做才能正确停止tomcat。
It's a real problem. 这是一个真正的问题。 Non-daemon threads pooled by your executor don't go away by themselves, if you don't call shutdown on the executor then the threads will stay alive and prevent the JVM from exiting. 执行程序池中的非守护程序线程不会自行消失,如果您不对执行程序调用shutdown,则线程将保持活动状态并阻止JVM退出。
Make a ServletContextListener that calls shutdown on your executor on contextDestroyed
. 创建一个ServletContextListener,以便在contextDestroyed
上的执行程序上调用shutdown。
Alternatively you could make the threads returned by your ThreadFactory daemon threads, so that they would exit when the last non-daemon thread terminates. 或者,您可以使ThreadFactory守护程序线程返回的线程成为最后一个非守护程序线程终止时退出。 Since you're doing data retrieval it would seem better to avoid this since it could leave database resources unclosed. 由于您正在执行数据检索,因此避免这种情况似乎更好,因为这可能会使数据库资源处于关闭状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.