本帖最后由 bugxing 于 2010-10-09 10:26:21 编辑

解决方案 »

  1.   

    wait已经把this上的锁释放了
    不管有没有返回,甚至在while(true)当中调用wait都会释放锁而且阻塞当前线程
    锁的释放不是看return的
      

  2.   

    你的锁是加在方法上的,A访问synchronized修饰的put方法挂起后,B应该是不能访问put方法的;synchronized锁住的是方法。
      

  3.   

    The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.JDK中的说明:wait的时候会释放得到的锁.
      

  4.   


    这是绝对错误的
    很多人对锁的理解都有偏差
    只有对象上才有监视器=monitor=锁public synchronized void put(){}
    public synchronized int get(){}
    public void other(){}线程t1进入put或get的前提是获得当前对象(也就是调用这些方法的对象主体)上的锁
    t1未释放锁,t2当然不能执行put/get
    但t1释放了锁,t2参与竞争并获得锁之后,就可以执行put/get中的内容synchronized 不是锁对象也不是锁方法,正确解读是:进入这个区块必须获得锁
    再看一种情况
    t1进入put期间,只能说等待put调用主体(所谓的当前对象)的锁的所有线程会因为等待锁而阻塞
    这个期间,任意线程,而且是任意多个线程都可以随意调用other方法清楚了吧
      

  5.   

    刚写了个试下,我之前对wait的理解错了,wait是可以释放加在方法上的锁的,a进入put在里面wait的时候,b可以进入put。