当前位置: 代码迷 >> J2SE >> 死锁没死成,该怎么解决
  详细解决方案

死锁没死成,该怎么解决

热度:83   发布时间:2016-04-24 12:49:06.0
死锁没死成
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!");            }        }    }}


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

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