File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoding deadlocks

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the practices to be followed for avoiding deadlocks?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Tutorial has a specialized tutorial chapter on this

./pope
 
David Ulicny
Ranch Hand
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I recognize deadlock in application?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has added doug Lea's widely appreciated util.concurrent package to it library.Checkout Threads for more
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'm using JProfiler but I missed the option Deadlock Detection.
Do you know how Deadlock Detection is working?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 867
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic