Advertisement



< Prev
Next >



Deadlock in Threads






Advertisement




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 -




< Prev
Next >
< Interthread Communication
Anonymous Threads >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement