public class TestDeadLock implements Runnable{
public int flag = 1;
static Object S1 = new Object(),S2=new Object();
public void run(){
System.out.println("flag="+flag);
if(flag==1){
synchronized(S1){
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
synchronized(S2){
System.out.println("1");
}
}
}
if(flag==0){
synchronized(S2){
try{
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
synchronized(S1){
System.out.println("0");
}
}
}
}
public static void main(String[] args){
TestDeadLock td1=new TestDeadLock();
TestDeadLock td2=new TestDeadLock();
td1.flag=1;
td2.flag=0;
Thread t1=new Thread(td1);
Thread t2=new Thread(td2);
t1.start();
t2.start();
}
}
//如果你把這個死鎖的執行過程都明白了,我想你的線程同步這一塊應該學得還算可以了!
posted on 2009-03-12 08:16
Werther 阅读(1824)
评论(2) 编辑 收藏 所属分类:
10.Java