简体   繁体   English

Tomcat停止时如何停止执行程序线程?

[英]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.

相关问题 如何在用户命令停止的Java命令提示符中停止线程? - How do you stop a thread in a Java command prompt that is stopped when the user commands it to stop? 带有Jersey和Tomcat的Java Web Services。 ¿当Web服务停止时如何停止所有线程? - Java Web Services with Jersey and Tomcat. ¿how to stop all the threads when the web service is stopped? 如何停止java executor类中的所有可运行线程? - How to stop all runnable thread in java executor class? 如何在运行后标识线程,然后使用executor插件在Grails中将其停止? - How to identify a thread after running and then stop it in Grails with the executor plugin? 如何中断执行程序线程 - How to interrupt executor thread Executor服务停止线程等待IO - Executor service stop thread waiting for IO 如何在停止tomcat服务器时检查线程是否被终止或继续运行? - How to check if thread is killed or keep running when you stop the tomcat Server? 即使执行程序服务线程的任务未完成,如何停止它们? - How to stop executor service threads even when their task is not completed? 如何知道线程何时停止或停止? - How to know when a thread stops or is stopped? 如何正常停止嵌入式Tomcat和tomcat仅在当前处理请求必须完成后才停止 - How to stop Embedded Tomcat gracefully and tomcat should get stopped only after the current processing requests have to complete
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM