???????????????????

????????????wait??notify??notifyAll?????????????????????????е???????????????????????????????????????????????????????????У?????????????????????????????????????????????wait???????????????wait set?У?????wait set??????????С?

???????????????????????????????????????????????????????????檔????????????????????????????????????????????

???????е?????????????????????????????????С??????????????????????ν???????????????????ν?????????????????????????????Щ??????????????????????????????β???????ν????????????????????????????????????????ж????????????????

??????????????????????????????????е?Guarded Suspension Pattern??????????

<pre class="brush:java;toolbar:false;">
        public synchronized void concreteWork()
{
   while(!condition)
   {
      try{
         wait();//???????????????this??????????????е??
       }catch(InterruptedException e)
       {  }
   }
  //????????????????ν????????е?
}
//?????????????wait?????wait??????????????ν?????

???????????????

????????????????????????????????????ν???????????????????????????????????Object??notifyAll??notify????????????notifyAll???????????????????????л???????????????????????е?????????й??????????????????

??????????????????????????????????????????????????????ν??????notify??????????????????????磬?????A???????????е??????ν??PA?????B???????????е??????ν??PB???????C?????????????PB????棬???????notify????????????A??A?????????PA???????棬????????????????B?????PB????棬??????????????????ε?????

???????notify???????????????“???е???????????????????ν??”??“??ζ??????????????????”??

????ReentrantLock??Lock?????????????????????????Condition??????????????????????С?????????????????????????????е???????????ν?????????????????й???????????????????ν?????ò???????????????????

??????????Condition?????Lock????????????????????????????????????й??????????????С??????Condition???????????????Lock?????Lock.newCondition?????????Lock??????????Condition??????Condition?У???Object??wait??notify??notifyAll??????????????await??signal??signalAll??

?????????Condition?????????????е????

???????????????????????У????????ù?????л???????????????????????????????????Condition??

????????????????????Condition??Lock???????н綹?棬??????????????????

package whut.usersynchrnoized;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
//????Condition??Lock??????н綹??
public class ConditionBoundedBuffer<T> {
    private final Lock lock = new ReentrantLock();
    private final Condition full = lock.newCondition();
    private final Condition empty = lock.newCondition();
    private final T[] items;
    private int tail?? head?? count;
    public ConditionBoundedBuffer(int size) {
        items = (T[]) new Object[size];
    }
    // ???????notfull
    public void put(T x) throws InterruptedException {
        lock.lock();
        try {
            while (count == items.length)
                full.await();// ??????????full?????????е??
            items[tail] = x;
            if (++tail == items.length)
                tail = 0;
            ++count;
            empty.signal();// ??λ?????????
        } finally {
            lock.unlock();
        }
    }
    // ???????notEmpty
    public T take() throws InterruptedException {
        lock.lock();
        try {
            while (count == 0)
                empty.await();// ??????????empty?????????е??
            T x = items[tail];
            items[tail] = null;
            if (++head == items.length)
                head = 0;
            --count;
            full.signal();// ??λ?????????
            return x;
        } finally {
            lock.unlock();
        }
    }
}