Deadlock

class A { 
synchronized void foo(B b) { 
String name = Thread.currentThread().getName(); 
System.out.println(name + " entered A.foo"); 
try { 
Thread.sleep(1000); 
} catch(Exception e) { 
System.out.println("A Interrupted"); 
} 
System.out.println(name + " trying to call B.last()"); 
b.last(); 
} 
synchronized void last() { 
System.out.println("Inside A.last"); 
} 
}

class B { 
synchronized void bar(A a) { 
String name = Thread.currentThread().getName(); 
System.out.println(name + " entered B.bar"); 
try { 
Thread.sleep(1000); 
} catch(Exception e) { 
System.out.println("B Interrupted"); 
} 
System.out.println(name + " trying to call A.last()"); 
a.last(); 
} 
synchronized void last() { 
System.out.println("Inside A.last"); 
} 
}

class Deadlock implements Runnable { 
A a = new A(); 
B b = new B(); 
Deadlock() { 
Thread.currentThread().setName("MainThread"); 
Thread t = new Thread(this, "RacingThread"); 
t.start(); 
a.foo(b); // get lock on a in this thread. 
System.out.println("Back in main thread"); 
} 
public void run() { 
b.bar(a); // get lock on b in other thread. 
System.out.println("Back in other thread"); 
} 
public static void main(String args[]) { 
new Deadlock(); 
} 
}

This is an example of deadlock, But I can’t understand where is the deadlock in that code?

The deadlock happened when two thread want to access one resource, But I don’t such thing in that coe

Please guide me

Wow - over a week and no comment yet!
Concurrency and race conditions are not easy to understand on first sight, but let me try to give some guidance.
When you compile and run the given example you get the following output:

RacingThread entered B.bar
MainThread entered A.foo
RacingThread trying to call A.last()
MainThread trying to call B.last()

So what does that tell you?
First of all you can see that neither “Back in main thread” nor “Inside A.last” and “Inside B.last” is printed out. The reason is simply that the main thread has run into a deadlock.

Now to the tricky part: Why is there a deadlock and how to to spot them?
The easiest way to reach enlightenment is to walk through the code the way the interpreter does.
So let’s start in the main method. There we create an instance of the Deadlock thread which means we are calling the constructor method.
In that constructor the start() method is called which then indirectly calls the run() method of the main thread. Now in the run() method b.bar(a); is called.
bar() (and foo(), and both last() methods) are synchronized which means that only one thread can have access to that method at a time!
In other words:
b.bar() calls the bar() method and locks b for other threads from access until the thread is finished.
After the run() method is done we return to the constructor where a.foo() is called and which blocks a from access of other threads.

The interesting part now is that foo() tries to call last() of the blocked b and bar() itself tries to call last() of blocked a.

So what to do?
To be honest - it depends on the situation.
In this case (provided that last() could be accessed concurrently without danger to internal data) you should remove the synchronized statement of both last() methods. Why? Because the last() methods are only called form inside of synchronized methods (foo() and bar()) and a synchronized access to both methods (foo() and bar()) is still granted.

When you remove the synchronized statement from both last() methods the output will lok like this:

RacingThread entered B.bar
MainThread entered A.foo
RacingThread trying to call A.last()
Inside A.last
Back in other thread
MainThread trying to call B.last()
Inside B.last
Back in main thread

I know it’s not easy to understand - and it’s not easy to explain either, but I hope I could have given you a hint. :slight_smile: