wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Any Solution of Deadlock? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Any Solution of Deadlock?" Watch "Any Solution of Deadlock?" New topic
Author

Any Solution of Deadlock?

Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

What's the best way to detect deadlock and remove deadlock?

Example:

public class RunDeadLock
{
public static void main(String[] args)
{
Thread1 t1= new Thread1();
Thread2 t2= new Thread2();

t1.start();
t2.start();

}
}


public class Thread1 extends Thread
{
public void run(){
ThreadResource1.go1();
}
}

public class Thread2 extends Thread
{
public void run(){
ThreadResource2.go2();
}
}


public class ThreadResource2
{
public static synchronized void go2(){
System.out.println("Entered in resource 2");
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(5000);
}catch(Exception e){}

ThreadResource1.go1();
System.out.println("Exiting from resource");


}
}


public class ThreadResource1
{
public static synchronized void go1(){
System.out.println("Entered in resource1");
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(5000);
}catch(Exception e){}
ThreadResource2.go2();
System.out.println("Exiting from resource");

}
}

How to remove deadlock?


Regards, Abdul Mohsin
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This question seems to be a general thread programming issue, rather than SCJP specific. Moving to our Threads forum. Interested SCJP'ers can follow there.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy, cowboys!

To recognize a lock look for threads calling other threads that may be blocked who call the calling thread by themselves.

In this example each synchronized static go-Method calls the go Method of the other class and vice versa.
And (important) it is calling it from synchronized code.
Perhaps somebody can provide a nice graphic for this...


Easiest solution:
get the call of go1/2 out of the sync background!

Extra tipp: only sleep 500 ms instead of 5000. Makes it much faster.

By the way: the code is still an endless loop (or better two endless loops). It will never end.


Yours,
Bu.


all events occur in real time
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi Hassel,

I know that code is in endless loop but before that it stucked in deadlock
I just want to remove this deadlock condition.
Please explain me what are the different ways to detect and resolve deadlock situation.

Regards,

Abdul Mohsin
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Originally posted by Abdul Mohsin:
Hi Hassel,

I know that code is in endless loop but before that it stucked in deadlock
I just want to remove this deadlock condition.
Please explain me what are the different ways to detect and resolve deadlock situation.

Regards,

Abdul Mohsin


Hi Abdul,
Addressing the two parts of your question individually:

Detecting deadlock:
In order to detect a deadlock, you have to take a thread dump of the JVM.
In order to analyze these thread dumps there are a few tools available. this article gives you some insight into one such tool. Googling will give you some more.

Removing deadlocks:
Well, there is no solution to bad programming but to change the program itself
So, you have to change the code to remove the deadlock. There is no way you can resolve a deadlock solution without changing the code.
Once you have correctly analyzed the thread dump, you can easily rectify such a situation.


apigee, a better way to API!
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi,

to make my above post work what changes you will do to remove deadlock situation.

Regards,

Abdul Mohsin
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638


to make my above post work what changes you will do to remove deadlock situation.

The above code, which i assume is just a demo code, is designed to cause a deadlock. The only way out is to make the methods un-synchronized OR to remove nested calls to synchronize methods in different threads.
Probably, if you have a business specific use case, then this exercise will make some sense.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Nitesh Kant:

The only way out is to make the methods un-synchronized OR to remove nested calls to synchronize methods in different threads.


Well, not exactly.

The way to avoid deadlock when locking multiple resources is to always lock them in the same order in every case. This program wouldn't deadlock if go1() and go2() were written as shown below. Note that both methods lock ThreadResource1.class before ThreadResource2.class; therefore only one method can proceed at a time, and the other must wait. Once a method locks ThreadResource1.class, it will always proceed to completion.



[Jess in Action][AskingGoodQuestions]
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Thanks for pointing that out Ernest. Missed the obvious in my last post.
Abdul Mohsin
Ranch Hand

Joined: Apr 26, 2007
Posts: 111

Hi Ernest ,

Excellent explaination !!

Thanks,

Abdul Mohsin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Any Solution of Deadlock?