public class TestDeadLock implements Runnable{ public int flag;
static Object o1=new Object(),o2=new Object();
/* (non-Javadoc)
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("flag="+flag);
if(flag==1){
synchronized(o1){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o2){
System.out.println("1");
}
}
if(flag==0){
synchronized(o2){
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
synchronized(o1){
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();
}
}

解决方案 »

  1.   


    public class TestDeadLock implements Runnable{    public int flag;
        static Object o1=new Object(),o2=new Object();
        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("flag="+flag);
            if(flag==1){
                synchronized(o1){
                    try{
                        Thread.sleep(500);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                    synchronized(o2){
                    System.out.println("1");
                }
                }
                
            }
            if(flag==0){
                synchronized(o2){
                    try{
                        Thread.sleep(500);
                    }catch(InterruptedException e){
                        e.printStackTrace();
                    }
                   synchronized(o1){
                    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();
        }
    }放在里面再去试试吧
      

  2.   


    public class Sisuo extends Thread {

    private Object one;
    private Object two;

    public Sisuo(Object one, Object two) {
    super();
    this.one = one;
    this.two = two;
    } @Override
    public void run() {
    // TODO Auto-generated method stub
    System.out.println(this.getName()+" 启动");

    int time1 = 0;
    int time2 = 0;
    while(true){
    synchronized (one) {
    time1++;
    System.out.println(this.getName()+" 锁住 "+one +" "+time1+"次");
    synchronized (two) {
    time2++;
    System.out.println(this.getName()+" 锁住 "+two +" "+time2+"次");
    }
    }
    }
    }

    public static void main(String[] args) {
    Test one = new Test("One");
    Test two = new Test("Two");
    Sisuo thread1 = new Sisuo(one,two);
    thread1.setName("Thread1");
    Sisuo thread2 = new Sisuo(two,one);
    thread2.setName("Thread2");
    thread1.start();
    thread2.start();
    }
    }
    class Test{
    private String name;
    public Test(String name) {
    // TODO Auto-generated constructor stub
    this.name = name;
    }
    @Override
    public String toString() {
    // TODO Auto-generated method stub
    return name;
    }
    }