难道线程的死锁==线程的等待???应该不是吧
RayBB 2007-04-06 02:03:51 死锁没有成功,不能找出错误原因
public class TestDeadLock implements Runnable {
int frag ;
static Object o1 = new Object(),o2 = new Object();
public void run(){
System.out.println("frag"+frag);
if(frag==1){
synchronized(o1){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o2){
System.out.print("ok2");
}
}
if(frag==0){
synchronized(o2){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o1){
System.out.print("ok1");
}
}
}
public static void main(String[] args) {
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.frag=1;
td2.frag=0;
Thread th1 = new Thread(td1);
Thread th2 = new Thread(td2);
th1.start();
th2.start();
}
}