aspose file tools
The moose likes Threads and Synchronization and the fly likes Is there a way to detect and break the deadlock Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Is there a way to detect and break the deadlock" Watch "Is there a way to detect and break the deadlock" New topic
Author

Is there a way to detect and break the deadlock

Om Tejas
Greenhorn

Joined: Dec 02, 2009
Posts: 27
Hi,

Deadlock is very common in Java. Is there a way to break the deadlock using any APIs.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35223
    
    7
Deadlock is very common in Java.

That's news to me. What are you basing this statement on?

Deadlocks don't occur for a particular language, they occur for particular code. Are you talking about specific code that exhibits deadlocks?


Android appsImageJ pluginsJava web charts
Om Tejas
Greenhorn

Joined: Dec 02, 2009
Posts: 27
Yes for a specific code. For example when using java threads.



If I am not wrong, the above is a potential deadlock scenario. Is there a way to break this deadlock, apart from Ctrl+c etc. Any APIs
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35223
    
    7
Not sure how you think a deadlock could occur, given that the threads synchronize on different objects. If the code in processB synchronized on A.class, and vice-versa, then you'd have a potential deadlock:



The way to avoid deadlocks is not to write code that exhibits http://en.wikipedia.org/wiki/Deadlock#Necessary_conditions. This is true for all code running in multi-threaded environments, irrespective of the language it's written in.
Om Tejas
Greenhorn

Joined: Dec 02, 2009
Posts: 27
The link and wiki gives information on necessary condition, prevention, avoidance and detection. My question what to do after deadlock is dectected. I have come across some articles which say that ThreadMxBean can be used to detect the deadlock threads. What I am trying to find out is there a way to move the threads in deadlock out of deadlock state once deadlock is detected.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3044
    
    1

Not within the same program, or it wouldn't be deadlock.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35223
    
    7
Stephan van Hulst wrote:Not within the same program, or it wouldn't be deadlock.

There could still be live threads, even if some other ones are deadlocked.

Killing threads and then resuming execution would be dangerous, as the application could be in an instable or inconsistent state (see the explanation of the Thread.destroy() method). Some high-end databases can detect deadlocks, select one thread for termination and let the others proceed, but that's a special case as the app can tell exactly what a thread is doing (by looking at the SQL it executes).
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2770
    
    2
If the threads involved are each using the synchronized keyword to lock on (or attempt to lock on) the various monitors involved, then once deadlock occurs, there's no way out other than maybe Thread.destroy(), as Ulf said.

However, since JDK 5 it's been possible to use java.util.concurrent.locks.Lock instead of the synchronized keyword. The resulting code is more verbose but has many more options for handling locks. In particular, if you never use the lock() method, but instead use lockInterruptibly() or tryLock(), then it becomes possible to write code that gets out of a prospective deadlock. Which technically means it wasn't a deadlock if you can get out of it. But you can get out of situations that would have otherwise been deadlocks if using synchronization. Writing such code would still be rather complex; I don't have a ready quick example. But at least it's possible.

Oh, and java.util.concurrent.locks.ReentrantLock has additional methods that could be useful. Things like getOwner(), getQueuedThreads(), isLocked(), isHeldByCurrentThread(), etc. Lots of possibilities there.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16681
    
  19


Generally, deadlocks are avoided, instead of "detected and broken". Even if deadlock detection is simple (and it isn't), the task of undoing what you did so far isn't easy. In order to break a deadlock, you need to undo operations, which means that you need to support some sort of transaction system -- which requires that databases, and everything you use, support transactions too.

And the most common way to avoid deadlocks is to establish a locking hierarchy -- when lots of related locks are needed, locks need to be grabbed in a certain order. It makes it much more coarse, but it will avoid deadlocks.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
jQuery in Action, 2nd edition
 
subject: Is there a way to detect and break the deadlock
 
Similar Threads
Thread Dumps
Skip finally block
WHy isnt there an Output?
Doubt on Thread - Is the code went in to DeadLock state ?
Deadlock