如何处理不停止线程的行为不当的库 [英] How to deal with badly behaved libraries that don't stop threads

查看:119
本文介绍了如何处理不停止线程的行为不当的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理关闭库时没有正确清理线程的第三方库时有哪些解决方法?

What are some workarounds when dealing with third party libraries that don't correctly clean-up threads when the library is shutdown?

许多库都暴露了生命周期方法,要么明确地或隐含地,对于其中包含的代码。例如,Web应用程序框架存在于servlet容器中的Web应用程序上下文中。创建上下文时,框架可能由于各种原因启动某些线程。

Many libraries expose lifecycle methods, either explicitly or implicitly, for the code contained therein. For example, a web application framework exists within a web application context in a servlet container. When the context is created, the framework may start some threads, for various reasons.

现在,进一步举例说明当servlet容器或Web应用程序上下文关闭时,Web应用程序框架应该终止所有这些线程。 ExecutorService 的创建者应该关闭库,或者应该制定一些其他停止这些线程的方法。

Now, taking the example further, when the servlet container or the web application context is shutdown, the web application framework should terminate all these threads. Either the ExecutorServices created by the library should be shutdown, or some other means of stopping those threads should be enacted.

最坏的情况是线程是非守护程序线程。这些实际上会停止Java进程的终止。但即使它们是守护程序线程,允许线程继续也可能是不好的做法。如果(回到示例)servlet容器嵌入在其他代码中,其他代码可能会继续运行,线程仍在继续运行,这可能会导致问题。

Worst case is that the threads are non-daemon threads. These would actually stop the termination of the Java process. But even if they are daemon threads, allowing threads to continue is probably bad practice. If (back to the example) the servlet container is embedded within other code that other code may continue to run with the threads still chugging away, which may cause problems.

没有以编程方式暴露这些线程,所以可以做些什么?

There's no programmatic way exposed to stop these threads, so what can be done?

如果你正在使用这样的第三方库,有什么方法可以强制关闭现有线程?

If you are using such a third party library, what are the ways to force existing threads to be shutdown?

推荐答案


如果您正在使用这样的第三方库,有什么方法可以强制关闭现有线程?

If you are using such a third party library, what are the ways to force existing threads to be shutdown?

一般来说,有没有安全的方法可以保证在所有情况下都能正常工作。您最接近安全解决方案的方法是使用 ThreadGroup 枚举所有现有线程,并使用 Thread.interrupt()告诉每个线程停止。当然,不能保证线程会注意,或者它们会在响应中断时干净地关闭。

In general, there is no safe way to do this that is guaranteed to work in all cases. The closest you are likely to get to a safe solution is to use the ThreadGroup to enumerate all existing Threads, and use Thread.interrupt() to tell each thread to go stop. Of course, there is no guarantee that the threads will pay attention, or that they will shutdown cleanly in response to an interrupt.

IMO,最好的策略是:

IMO, the best strategy is:


  • 如果线程不需要干净地关闭,那么拔掉JVM上的插件调用System.exit()

  • if the threads do not need to be shut down cleanly, then pull the plug on the JVM by calling System.exit()

如果线程需要干净地关闭以避免损坏,那么不要使用库...或者自己解决问题。

if the threads need to be shut down cleanly to avoid the possibility of damage, then don't use the library ... or fix the problem yourself.

这篇关于如何处理不停止线程的行为不当的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆