File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Avoding deadlocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Avoding deadlocks" Watch "Avoding deadlocks" New topic
Author

Avoding deadlocks

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

What are the practices to be followed for avoiding deadlocks?


Groovy
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Java Tutorial has a specialized tutorial chapter on this

./pope


blog - InfoQ.com
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
How can I recognize deadlock in application?


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
At least two threads waiting for locks.
Thread A - lock ObjA, waiting to lock ObjB
Thread B - lock ObjB, waiting to lock ObjA

the simplest example

./pope
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Yes, I know what deadlock is, but how can I detect him in the application?
How can I say: "In this method is a deadlock?"
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Java has added doug Lea's widely appreciated util.concurrent package to it library.Checkout Threads for more
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by David Ulicny:
Yes, I know what deadlock is, but how can I detect him in the application?
How can I say: "In this method is a deadlock?"


Involving a profiler as JProfiler or OptimizeIT.

./pope
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Thanks, I'm using JProfiler but I missed the option Deadlock Detection.
Do you know how Deadlock Detection is working?
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by David Ulicny:
Thanks, I'm using JProfiler but I missed the option Deadlock Detection.
Do you know how Deadlock Detection is working?


Not by heart but I think you may try to look for this into the help.

./pope
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
What are the practices to be followed for avoiding deadlocks?
Deadlock can arise if four conditions hold simultaneously, so you can check these conditions with your program carefully.
Mutual exclusion: only one process can use a resource at a time

Hold and wait: a process holding at least one resource and also is waiting to acquire additional resources held by another's process.

No preemption: a resource can be released only voluntarily by the process holding it, after that process has completed its task.

Circular wait: there exists a set {p0,p1,p2,.....pi} of waiting processes such that pi is waiting for a resource that is held by p1, p1 is waiting for a resource that is held by p2,....pi-1 is waiting for a resource that is held by pi and pi is waiting for a resource that is held by p0.


Francis Siu
SCJP, MCDBA
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I get a newsletter from somebody who wrote an analyzer that can detect deadlock situations. Guess I'll have to read how he did it some day. As I recall it is slow enough that it's a debugging tool, not something to leave in production code.

One rule is to always lock resources in the same order. If T1 is holding A trying to get B and T2 is holding B trying to get A we're in trouble. If all threads get A first, then B they can coexist pretty happily. I saw a utility - maybe in Doug Lea's library? - that given a list of resources obtains locks in an order determined by comparators. So if T1 asked for B,A,C and T2 asked for C,B it would get the locks in A,B,C order for both threads and avoid deadlock.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18526
    
  40

Originally posted by Pradeep Bhat:
What are the practices to be followed for avoiding deadlocks?


Deadlocks are best avoided by programming practices. Common techniques are to establish a locking order, or a higher order lock. It is a very important topic, and we spend a whole chapter to discuss it.

Another technique is to use optimistic locking techniques. After all, what better way to avoid deadlocks, than not locking at all...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18526
    
  40

Originally posted by David Ulicny:
How can I recognize deadlock in application?


In Java Threads, we actually develop a class that can be used to detect a deadlock. Even so, it is not completely reliable. The issue is because, a "deadlock" may be caused by other causes than locks, it could even be caused by two threads waiting on variables to be in particular states.

In any case, feel free to download the source code from http://www.oreilly.com/catalog/jthreads3, if you are interested in deadlock detection. The source for the deadlock detecting lock is in the chapter 6 package.

Henry
 
wood burning stoves
 
subject: Avoding deadlocks
 
Similar Threads
How to prevent deadlock in java
problem -- Java-level deadlock
ecperf problems
Hi, All i have a doubt
deadlock