Tomcat 7 and ScheduledExecutorService.shutdown

你。 提交于 2020-01-03 11:18:05

问题


I am using ScheduledExecutorService to run scheduled threads.
I implemented ServletContextListener.contextDestroyed and invoked ScheduledExecutorService.shutdownNow and awaitTermination.

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:

SEVERE: The web application [/servlet] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.

Can this log be ignored? Or I am doing something wrong?

Thanks


回答1:


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.

Also, you shutdownNow() should return you the list of Tasks that still await termination (see JavaDoc). You could build logic to wait more if list is not empty.




回答2:


You are correctly shutting down your ScheduledExecutorService. However threads created by ExecutorService by default follow this naming convention: pool-X-thread-Y.

Timer-0 threads are created by Timer class. Look for them in your code and libraries.



来源:https://stackoverflow.com/questions/9930624/tomcat-7-and-scheduledexecutorservice-shutdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!