Program On DeadLock:
import java.util.*;
class A{
public synchronized void d1(B b)
{
System.out.println("Thread start executing d1() method");
try{
Thread.sleep(5000);
}catch(Exception e){}
System.out.println("Thread try to invoke B's last()");
b.last();
}
public synchronized void last()
{
System.out.println("last() method of A's class");
}
}
class B{
public synchronized void d2(A a)
{
System.out.println("Thread start executing d2() method");
try{
Thread.sleep(5000);
}catch(Exception e){}
System.out.println("Thread try to invoke A's last()");
a.last();
}
public synchronized void last()
{
System.out.println("last() method of B's class");
}
}
class DeadLock extends Thread{
A a=new A();
B b=new B();
public void m1(){
this.start();//internally call run() and allocate separate call stack,
a.d1(b);//execute By main Thread
}
public void run()
{
b.d2(a);
}
public static void main(String[] args)
{
DeadLock l=new DeadLock();
l.m1();
}
}
Comments
Post a Comment