日期:2014-05-20  浏览次数:20777 次

死锁没死成
Java code

public class TestDeadLock implements Runnable{
    public int flag = 0;
    public static Object o1 =new Object();
    public static Object o2 = new Object();
    public static void main(String[] args) {
        TestDeadLock t = new TestDeadLock();
        TestDeadLock tt = new TestDeadLock();
        t.flag = 0;
        tt.flag = 1;
        Thread t1 = new Thread(t);
        Thread t2 = new Thread(tt);
        
        t1.start();
        t2.start();
        
    }
    
    public void run() {
        System.out.println("flag ="+ flag);
        if (flag == 1) {
            synchronized(o1){
                try {
                    Thread.sleep(1000);
                }catch(InterruptedException e) {
                    System.out.println("fail!");
                }
            }
            synchronized(o2) {
                System.out.println("thread1 is close!");
            }
        } else {
            synchronized(o2){
                try{
                    Thread.sleep(5000);
                }catch(InterruptedException e) {
                    System.out.println("fail!");
                }
            }
            synchronized(o1) {
                System.out.println("thread2 is close!");
            }
        }
    }
}



------解决方案--------------------
呵呵 马士兵的死锁
------解决方案--------------------
问题解决了,来晚了啊!
给楼主捧个场
------解决方案--------------------
原来解决了啊 ~
------解决方案--------------------
恭喜楼主把问题解决~~
------解决方案--------------------
我喜欢这个名字:没死成!

呵呵,恭喜已经解决
------解决方案--------------------
恭喜了,
说实话这个问题俺解决不了,但是给了俺学习的引子。