Deadlock detection in Java

后端 未结 16 1046
野的像风
野的像风 2020-11-28 20:21

Long time ago, I saved a sentence from a Java reference book: \"Java has no mechanism to handle deadlock. it won\'t even know deadlock occurred.\" (Head First Java 2nd E

相关标签:
16条回答
  • 2020-11-28 20:55

    In general java does not offer deadlock detection. The synchronized keyword and built in monitors make it somewhat more difficult to reason about deadlock than in languages with explicit locking.

    I would suggest migrating to using java.util.concurrent.Lock locks and the like in order to make your locking schemes easier to reason about. In fact you could easily make your own implementation of the lock interface with deadlock detection. The algorithm is to basically traverse the lock dependency graph and look for a cycle.

    0 讨论(0)
  • 2020-11-28 21:00

    JConsole is able to detect deadlocks in a running application.

    0 讨论(0)
  • 2020-11-28 21:00

    Java 1.8 onwards, you can easily find if your program has a deadlock by using jcmd command on your terminal.

    1. Run jcmd on your terminal: It lists all the programs (PID and name) which are using Java.
    2. Now, run jcmd <PID> Thread.print: This will print the thread dump on your console and will also print if your program has a deadlock.

    You can analyse your Java program with more jcmd options listed by command jcmd <PID> help

    0 讨论(0)
  • 2020-11-28 21:02

    Java 5 introduced ThreadMXBean - an interface that provides various monitoring methods for threads. ... The difference is that findDeadlockedThreads can also detect deadlocks caused by owner locks (java.util.concurrent), while findMonitorDeadlockedThreads can only detect monitor locks (i.e. synchronized blocks)

    Or you can detect it programatically, refer this https://dzone.com/articles/how-detect-java-deadlocks

    0 讨论(0)
  • 2020-11-28 21:03

    Deadlocks can be avoided if you follow a simple rule: have all threads claim and release their locks in the same order. In this way, you never get into a situation where a deadlock can occur.

    Even the dining philosophers problem can be seen as a violation of this rule as it uses relative concepts of left and right spoon which result in different threads using different allocation orders of the spoons. If the spoons were numbered uniquely and the philosophers all tried to get the lowest numbered spoon first, deadlock would be impossible.

    In my opinion, prevention is better than cure.

    This is one of the two guidelines I like to follow to ensure threads work properly. The other is ensuring each thread is solely responsible for its own execution as it's the only one fully aware of what it's doing at any point in time.

    So that means no Thread.stop calls, use a global flag (or message queue or something like that) to tell another thread you want action taken. Then let that thread do the actual work.

    0 讨论(0)
  • 2020-11-28 21:08

    Not exactly what you asked, but when a deadlock does occur, you can do a "kill -3" on the process id and it dumps a thread dump to stdout. Also, the 1.6 jvm has some tools to do the same thing in a gui manner.

    0 讨论(0)
提交回复
热议问题