Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Any Solution of Deadlock?

 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards,

Abdul Mohsin
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing that out Ernest. Missed the obvious in my last post.
 
Abdul Mohsin
Ranch Hand
Posts: 111
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest ,

Excellent explaination !!

Thanks,

Abdul Mohsin
 
Ivan Korhner
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You may find this link interesting: http://korhner.github.io/java/multithreading/detect-java-deadlocks-programmatically/

It's a simple way to detect Java deadlocks programmatically and you can use it for example to print deadlocked threads to console with stack trace. It can really help while experimenting and studying multithreaded code.

Good luck!
 
imran sujoy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the same line, I have the following code which I felt should fall in a deadlock situation, but it does not. Could you please explain why this is not going into deadlock situation?


As many time I ran this code, I got the following output:
 
Piet Souris
Ranch Hand
Posts: 1157
19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Imran,

first of all, I changed 'print' to 'println':

you get the following output:
sb1 from T1: A
sb2 from T1: B
sb1 from T2: AD
sb2 from T2: BC

So, Thread1 is simply so fast that it is finished before a deadlock arises.
Now try this:

And now you have your deadlock.

Greetz,
Piet
 
imran sujoy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Piet.
Ok, so do you mean, that whether the two threads fall into a deadlock is somewhat dependent on how fast the threads are executing, so if I have hundreds of other processes in a synchronization block, that can make the threads to fall in deadlock situation? So, is this underlying system dependent behavior?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic