DeadLock condition of Thread

import java.lang.Thread;
import java.lang.System;
import java.lang.InterruptedException;
class A
{
synchronized void methodA( B bobj)
{
String name=Thread.currentThread().getName();
System.out.println(name+" Entered method A");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("Thread Interrupted ");
}
System.out.println(name+ " Trying to call the method callOut() from B");
bobj.callOut();
}
synchronized void callOut()
{
System.out.println( "Calling out from A");
}
}
class B
{
synchronized void methodB( A aobj)
{
String name=Thread.currentThread().getName();
System.out.println(name+" Entered method B");
try
{
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("Thread Interrupted ");
}
System.out.println(name+ " Trying to call the method callOut() from A");
aobj.callOut();
}
synchronized void callOut()
{
System.out.println( "Calling out from B");
}
}

class DeadLock implements Runnable
{
A aobj=new A();
B bobj=new B();

DeadLock()
{
Thread.currentThread().setName("MainThread ");
Thread t=new Thread(this,"RacingThread");
t.start();
aobj.methodA(bobj);
System.out.println("Back to main thread ");
}
public void run()
{
bobj.methodB(aobj);
System.out.println("Back to other thread ");
}
public static void main( String args[])
{
new DeadLock();
}
}

Comments

Popular Posts