public class Deadlock {
public static void main(String[] args) {
// TODO Auto-generated method stub
Dead d1=new Dead();
Thread t1=new Thread(d1);
Thread t2=new Thread(d1);
t1.setName("d1");
t2.setName("d2");
t1.start();
t2.start();
}
}
class Dead implements Runnable{
int flag;
public void run(){
if(Thread.currentThread().getName().equals("d1")){
m();
}
if(Thread.currentThread().getName().equals("d2")){
n();
}
}
A a=new A();
B b=new B();
public void init(){
Thread.currentThread().setName("main thread");
//invoke foo method of a's instance
a.foo(b);
System.out.println("entered the main thread later ");
}
public void run() {
// TODO Auto-generated method stub
Thread.currentThread().setName("assistant thread");
b.Bar(a);
System.out.println("entered the assistant thread later");
}
public static void main(String[] args) {
DeadLock dl=new DeadLock();
new Thread(dl).start();
//execute init method as new thread
dl.init();
}