English 中文(简体)
How to kill deadlocked threads in Java?
原标题:

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.

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.

最佳回答

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

See also: "Java Thread Primitive Deprecation".

问题回答

The java.util.concurrent.Lock.lockInterruptibly() method is interruptable, the far more common synchronized locking is not. As mentioned in the documentation, the ability to enumerate deadlocked threads is intended as a debugging aid and not as a means to recover in a production environment.

In production it is probably safer to exit the whole process and restart.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签