2012-03-29 75 views
5

我正在使用ScheduledExecutorService来运行预定线程。
我实施了ServletContextListener.contextDestroyed并且调用了ScheduledExecutorService.shutdownNowawaitTerminationTomcat 7和ScheduledExecutorService.shutdown

下面是一个例子:

重度::

@Override 
public void contextDestroyed(ServletContextEvent servletcontextevent) { 
    pool.shutdownNow(); // Disable new tasks from being submitted 
    try { 
     // Wait a while for existing tasks to terminate 
     if (!pool.awaitTermination(50, TimeUnit.SECONDS)) { 
     pool.shutdownNow(); // Cancel currently executing tasks 
     System.err.println("Pool did not terminate"); 
     } 
    } catch (InterruptedException ie) { 
     // (Re-)Cancel if current thread also interrupted 
     pool.shutdownNow(); 
     // Preserve interrupt status 
     Thread.currentThread().interrupt(); 
    }   
} 


尽管如此,我从Tomcat 7得到以下错误Web应用[/ servlet的]似乎已经开始了 线程名称为[Timer-0]但无法停止它。这很可能是 创建内存泄漏。

该日志可以忽略吗?或者我做错了什么?

感谢

+0

我的意思是,我的想法是:你的清理责任在哪里停止?还有什么可以做的呢?用消防水带打开箱子? 非常认真,我的意思是说,它看起来像你正在做的一切你能做的......我会忽略这个错误。 – ControlAltDel 2012-03-29 17:51:41

+0

你可以参考我以前的文章:) http://stackoverflow.com/questions/9926356/scheduledexecutorservice-when-shutdown-should-be-invoked – lili 2012-03-30 13:07:20

回答

4

您确定此错误与您的线程池有关吗?通过线程名'Timer-0'来判断它可能是由某种计时器启动的。

另外,您shutdownNow()应该返回您仍然在等待终止的任务列表(请参阅JavaDoc)。如果list不是空的,你可以建立逻辑来等待更多。

1

你正确地关闭您的ScheduledExecutorService。不过,默认情况下由ExecutorService创建的线程遵循此命名约定:pool-X-thread-Y

Timer-0线程由Timer类创建。在您的代码和库中查找它们。