What are some workarounds for working with third-party libraries that do not properly clean threads when the library shuts down?
Many libraries provide lifecycle methods, explicitly or implicitly, for the code contained in it. For example, a web application infrastructure exists in the context of a web application in a servlet container. When a context is created, the environment may trigger some threads for various reasons.
Now, taking an example further, when the servlet container or web application context is turned off, the web application infrastructure must terminate all these threads. Either the ExecutorService created by the library must be disabled, or some other way to stop these threads must be accepted.
In the worst case, threads are non-daemon threads . This would actually stop the termination of the Java process. But even if they are daemon threads, allowing threads to continue is probably a bad practice. If (back to the example), the servlet container is embedded in another code that may continue to run different code with threads still repulsive, which can cause problems.
There are no programmatic ways to stop these threads, so what can be done?
If you use such a third-party library, what are the ways to force the disconnection of existing threads?
Dan gravell
source share