Servlet "has started a thread but failed to stop it" - memory leak in Tomcat Servlet "has started a thread but failed to stop it" - memory leak in Tomcat multithreading multithreading

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


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.