本帖最后由 atnice 于 2012-07-18 22:01:20 编辑

解决方案 »

  1.   

    改了下class Test implements Runnable {
    private boolean flag; Test(boolean flag) {
    this.flag = flag;
    } public void run() {
    if (flag) {
    synchronized (MyLock.locka) {
    System.out.println("if locka");
    synchronized (MyLock.lockb) {
    System.out.println("if lockb");
    }
    }
    } else {
    synchronized (MyLock.lockb) {
    System.out.println("else lockb");
    synchronized (MyLock.locka) {
    System.out.println("else locka");
    }
    }
    }
    }
    }class MyLock {
    static Object locka = new Object();
    static Object lockb = new Object();
    }public class LockTry {
    public static void main(String[] args) {
    Thread t1 = new Thread(new Test(true));
    Thread t2 = new Thread(new Test(false));
     t1.start();
    t2.start();
    }
    }
    但你这个代码用if else锁得住吗 
      

  2.   

    是不是类名称的问题,定义main函数的类一定要和文件同名才可以啊
      

  3.   

    main函数的类和文件同名的
    编译能通过的,就是运行的时候出现提示
    Exception in thread "main" java.lang.NoSuchMethodError: Test.<init>(Z)V
            at DeadLockTest.main(DeadLockTest.java:50)我把 Thread t1 = new Thread(new Test(true));
       Thread t2 = new Thread(new Test(false));
       t1.start();
       t2.start();这些都注释掉,改成一条输出语句,运行就没问题的,应该是main里的方法有问题,可我查不出啊