Servlet “has started a thread but failed to stop it” - memory leak in Tomcat

前端 未结 1 1017
孤城傲影
孤城傲影 2020-12-09 04:54

Apache Tomcat says many times:

The web application [/MyServlet] appears to have started a thread named [pool-61-thread-2] but has failed to stop it. T

相关标签:
1条回答
  • 2020-12-09 05:03

    Yes, it's a problem. If your code starts non-daemon threads then those threads will continue working until they exit their run method. Even if everything else finishes, the old JVM will hang around while those threads continue on. If you start up a new instance then you can have a situation where the old threads are still working alongside the ones created by the new instance.

    The tasks need to be designed so that they will be responsive to interruption (as opposed to eating the exception and going on, which is what your example shows). That means checking the interrupted flag on the current thread, and catching InterruptedException in a helpful way that allows the task to break its work off and also resets the interrupted flag if needed. ExecutorService implementations have a shutdownNow method that will interrupt the current tasks.

    Here's an example of how to stop a thread using interruption.

    Make sure the executor gets shut down, you can handle this in a ServletContextListener.

    0 讨论(0)
提交回复
热议问题