java死锁问题
public class app12{public static void main(String[] args){
Runner r1 = new Runner();
Runner r2 = new Runner();
r1.flag = 0;
r2.flag = 1;
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
}
}
class Runner implements Runnable{
int flag = 1;
Object o1 = new Object();
Object o2 = new Object();
public void run(){
System.out.println("flag---"+flag);
if(flag==1){
synchronized(o1){
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o2){
System.out.println("true");
}
}
if(flag==0){
synchronized(o2){
try{
Thread.sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o1){
System.out.println("false");
}
}
}
}
为什么没有死锁!!