如何在Java中杀死死锁线程? [英] How to kill deadlocked threads in Java?
问题描述
我想杀死陷入死锁状态的线程。首先,我们可以使用 findDeadlockedThreads()<检测处于死锁状态的线程ID / code>
中的方法
java.lang.management
。
I'd like to kill threads that are stuck in deadlock state. First, we can detect thread ids in deadlock state using the findDeadlockedThreads()
method of the ThreadMXBean
class in java.lang.management
.
然后,我想通过线程ID杀死线程,因此我有两个相关的问题:
(1)如何获得线程的控制权通过线程ID?
(2)如何杀死被阻止的线程?我认为invokting interrupt()方法会给线程一个异常并将终止该线程。
Then, I'd like to kill the threads by thread ids, and thus I have two related questions:
(1) How to get the control of a thread by thread id?
(2) How to kill a blocked thread? I think that invokting interrupt() method will give an exception to the thread and will kill the thread.
推荐答案
从根线程组,您可以使用Thread类枚举所有正在运行的线程。然后你可以在与你的ID相匹配的那个上调用Thread.stop。
From the root threadgroup, you can have the Thread class enumerate all running threads. Then you can call Thread.stop on the one that matches your ID.
尽管如此,由于可能会使对象处于不一致的状态,这是非常危险的。我不相信中断方法会导致线程被释放而等待同步锁定,所以(邪恶)停止方法
Having said that, this is highly dangerous due to the potential to leave objects in an inconsistent state. I don't believe that the interrupt method will cause a thread to be freed up that is stuck in waiting on a synchronization lock, so the (evil) stop methods
参见还有: Java Thread Primitive Deprecation 。
这篇关于如何在Java中杀死死锁线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!