简体   繁体   English

Tomcat 7和ScheduledExecutorService.shutdown

[英]Tomcat 7 and ScheduledExecutorService.shutdown

I am using ScheduledExecutorService to run scheduled threads. 我正在使用ScheduledExecutorService运行计划的线程。
I implemented ServletContextListener.contextDestroyed and invoked ScheduledExecutorService.shutdownNow and awaitTermination . 我实现了ServletContextListener.contextDestroyed并调用了ScheduledExecutorService.shutdownNowawaitTermination

Here is an example: 这是一个例子:

@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();
    }        
}


Still, I am getting the following error from Tomcat 7: 不过,我仍然从Tomcat 7中收到以下错误:

SEVERE: The web application [/servlet] appears to have started a thread named [Timer-0] but has failed to stop it. 严重:Web应用程序[/ servlet]似乎已启动名为[Timer-0]的线程,但未能停止它。 This is very likely to create a memory leak. 这很可能造成内存泄漏。

Can this log be ignored? 可以忽略此日志吗? Or I am doing something wrong? 还是我做错了什么?

Thanks 谢谢

Are you sure this error is related to your Thread pool? 您确定此错误与您的线程池有关吗? Judging by thread name 'Timer-0' it probably had been started by some sort of timer. 从线程名称“ Timer-0”判断,它可能是由某种计时器启动的。

Also, you shutdownNow() should return you the list of Tasks that still await termination (see JavaDoc). 另外,您的shutdownNow()应该返回仍在等待终止的Task列表(请参阅JavaDoc)。 You could build logic to wait more if list is not empty. 如果列表不为空,则可以构建逻辑以等待更多。

You are correctly shutting down your ScheduledExecutorService . 您正在正确关闭ScheduledExecutorService However threads created by ExecutorService by default follow this naming convention: pool-X-thread-Y . 但是,默认情况下,由ExecutorService创建的线程遵循以下命名约定: pool-X-thread-Y

Timer-0 threads are created by Timer class. Timer-0线程由Timer类创建。 Look for them in your code and libraries. 在您的代码和库中查找它们。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM