aspose file tools*
The moose likes Java in General and the fly likes How to prevent deadlock in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to prevent deadlock in java" Watch "How to prevent deadlock in java" New topic
Author

How to prevent deadlock in java

Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
Hi,
How do i detect and prevent Deadlocks in JAVA.


SCJP 5.0 77%
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Have a read of this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Short answer: a deadlock occurs when one thread has the monitor for A and tries to get the monitor for B while another thread has the monitor for B and tries to get the monitor for A. Each will wait forever for the other to give up a lock. So one trick is to always access resources in the same order. If thread 1 tries to get A then B and thread 2 tries to get A then B they'll eventually get what they need and complete. That solution is too simplistic for some complex applications but might help in small subsystems.

If you want to dig really deep, see the JavaSpecialists Newsletters. This guy usually make my head explode.



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
 
Don't get me started about those stupid light bulbs.
 
subject: How to prevent deadlock in java