This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Avoiding deadlock situations in java  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone suggest some good points on how to avoid deadlock situation in java programming?
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this page from The Java Tutorial...

http://java.sun.com/docs/books/tutorial/essential/threads/deadlock.html
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a sure-fire way to avoid deadlocks. Don't spin-off any threads! It's hard, error prone, don't do it. But seriously, you really have to think about why you need threads that could potentially run you into a deadlock situation.

--Dale--
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classic deadlock is when one thread has a lock on A and tries to lock B while another thread has a lock on B and tries to lock A. One way to avoid this is to always grab resources in the same order. You may want to hide a set of resources behind a manager that assures that locks are acquired in order.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a sample example to show the deadlock situation which may give a
clear idea

<code>
/**
* Thread-1 acquires a lock on lock1 but needs lock2.
* Thread-2 has acquired a lock on lock2 and needs to acquire lock1. Neither thread will
* yield and neither will progress. This is deadlock. Figure 4-7 shows the output.
* the output is:
* Thread-1:locked 2, trying to get lock1
* Thread-0:locked 1, trying to get lock2
*/
public class DeadLockExample {

public static void main(String args[]){
DeadLockExample dle = new DeadLockExample();

Object lock1 = new Object();
Object lock2 = new Object();

Runner1 thread1 = dle.new Runner1();
Runner2 thread2 = dle.new Runner2();

thread1.lock1 = lock1;
thread2.lock1 = lock1;
thread1.lock2 = lock2;
thread2.lock2 = lock2;

thread1.start();
thread2.start();
}

/**
* Lock object 1 then wait for object 2.
*/
class Runner1 extends Thread{
public Object lock1;
public Object lock2;

public void run(){
synchronized(lock1){
delay();
String msg = ":locked 1, trying to get lock2";
System.out.println(Thread.currentThread().getName()+ msg);
synchronized(lock2) {
System.out.println("locked 2");
}
}
}
}

/**
* Lock object 2 then wait for object 1.
*/
class Runner2 extends Thread{
public Object lock1;
public Object lock2;

public void run(){
synchronized(lock2){
delay();
String msg =":locked 2, trying to get lock1";
System.out.println(Thread.currentThread().getName() + msg);
synchronized(lock1){
System.out.println("locked 1");
}
}
}
}

/**
* build in a delay
*/
private static void delay(){
try{
String msg = Thread.currentThread().getName() + " elay";
Thread.currentThread().sleep((long)(Math.random()*1000));
}catch(InterruptedException ie){
ie.printStackTrace();
}
}

}

</code>

I hope this may help
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!