• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Avoding deadlocks

 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are the practices to be followed for avoiding deadlocks?
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java Tutorial has a specialized tutorial chapter on this

./pope
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I recognize deadlock in application?
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?"
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic