This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Avoiding deadlock situations in java  RSS feed

 
Naina Si
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?
 
marc weber
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
 
Dale Seng
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--
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Stan James
(instanceof Sidekick)
Ranch Hand
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.
 
Alan Mehio
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!