Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to prevent deadlock in java

 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How do i detect and prevent Deadlocks in JAVA.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a read of this.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic