< Prev
Next >



Deadlock in Threads








Creating a deadlock situation -:


We will create a deadlock situation in the upcoming code by having two threads -

class A 
{

synchronized public void m(A a,B b)
{

System.out.print(Thread.currentThread().getName());
System.out.println(" thread is in A'a m() method, trying to call B's m() method");
b.m(a,b);	//Line1
}
}


class B 
{

synchronized public void m( A a,B b)
{

System.out.print(Thread.currentThread().getName());
System.out.println(" is in B's m() method, trying to call A's m() method");
a.m(a,b);	//Line2
}
}


class C implements Runnable
{
A a= new A(); //Line3
B b= new B(); //Line4

Thread t;

C()
{
t= new Thread(this, "Thread2"); //Line5
t.start();	//Line6
a.m(a,b);   //Line7
}

public void run()
{
b.m(a,b); //Line8
}


public static void main(String []ar)
{
 new C(); //Line9
}

}



Output is - :


main thread is in A'a m() method, trying to call B's m() method
newThread is in B's m() method, trying to call A's m() method


Program Analysis





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Interthread Communication
Anonymous Threads >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
Python

Ad2