*
The moose likes Threads and Synchronization and the fly likes Deadlock query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Deadlock query" Watch "Deadlock query" New topic
Author

Deadlock query

Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
Hi all,
If there happens a deadlock in a code. Are there any preventive measures to cope up with it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41092
    
  44
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.


Ping & DNS - my free Android networking tools app
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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!


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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

Joined: Jan 30, 2000
Posts: 18671
As far as I know, exiting the JVM is your only real option there.


"I'm not back." - Bill Harding, Twister
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Mar 31, 2006
Posts: 43


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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Feb 25, 2007
Posts: 1638

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.
 
jQuery in Action, 2nd edition
 
subject: Deadlock query
 
Similar Threads
Doubt on Thread - Is the code went in to DeadLock state ?
urlyBird 1.2.3
How To Avoid Deadlock?
is this call synchronized ?
deadlock