• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Deadlock query

 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
If there happens a deadlock in a code. Are there any preventive measures to cope up with it?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 4 conditions necessary for a deadlock to occur. Preventing the possibility of all 4 happening at the same time should prevent deadlocks. See WikiPedia: Deadlock for details, especially the "Necessary conditions", "Avoidance" and "Prevention" sections.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That article is certainly comprehensive, but not a quick read.

To me, "deadlock" in Java normally means deadlock of thread synchronisation. This typically happens when more than one lock is involved in some operations and different threads lock the locks in different orders. The solution to this problem is to impose a synchronisation strategy on your code, so that locks are always acquired in the same order.

That is, however, only one type of deadlock and one way of preventing it. Don't let me stop you reading up on all the others!
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks a lot to both of you. Please inform if deadlock has already occured in a code during run, then are there any ways to get rid of it or we have to forcefully exit? I was asked in an interview , what you will do if deadlock has already taken place? I answered if it has already happened i'll have to restart my terminal
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, exiting the JVM is your only real option there.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdk 5 onwards, if the code uses any implementation of java.util.concurrent.locks.Lock and lock using method lockInterruptibly(). Deadlock can be broken by sending an interrut signal to the threads.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. I was thinking only of deadlock with sync block - once that happens, you're stuck. That's one of the advantages of the java.util.concurrent stuff.
 
sarvesh meens
Ranch Hand
Posts: 43
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nitesh Kant : Jdk 5 onwards, if the code uses any implementation of java.util.concurrent.locks.Lock and lock using method lockInterruptibly(). Deadlock can be broken by sending an interrut signal to the threads


* Good point. But this can only be used if deadlock is detected. Is it possible to detect a deadlock programmatically?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know a good way to test definitively if thwo threads are in deadlock. But a general strategy might be to keep a list of all the threads you have that are performing tasks, and what time they started on their current task. If one task takes much longer than expected, it may be deadlocked. You can test if thhread.getState == Thread.State.BLOCKED. You might also test if thread.getStackTrace() returns the exact same contents several times in a row. If a thread stays BLOCKED for a long time, with now change in getStackTrace(), there's a very good chance it's in deadlock. Or even if not deadlock, there's a good chance something is wrong, and it's time to interrupt it and try again.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sarvesh meens:

* Good point. But this can only be used if deadlock is detected. Is it possible to detect a deadlock programmatically?


I was just surfing the net for some other stuff and i got to this article
It uses java.lang.management package to find deadlocked threads.
I thought it will be of some use for you, so am posting the link.

DISCLAIMER: I have not tried the code but must be working because it uses the standard jdk api.
May be someone else who has used this will be able to comment.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic