????Java???????????????????????????????????????wait???????????????????????????? ???????????????????????????????????
????wait????÷???????????????????????
????synchronized (lock) {
????while (/*????*/) {
????try {
????lock.wait();
????} catch (InterruptedException e) {
????e.printStackTrace();
????}
????}
????//???????????????????????
????1). lock.wait()????????????????????????е?????????????????л?????????????
????2). ????ж????????????????while??????if?????????????????????????????????????
????3). notify ?? notifyAll??????notify???????????????????? notifyAll????????????????????????????????????   notify??????????????????????? ????????????????????
????????????????????? notify??notifyAll??????????Щ???????????????????2?????????????????????У??????±????Ч???
???????????? ???????stack?????????size >= 1????????????Щ???stack??????????????е?????
class SaboStack2 {
private final int[] _data;
private int size = 0;
public SaboStack2(int limit) {
_data = new int[limit];
}
private final ReentrantLock lock = new ReentrantLock();
private final Condition lock_empty = lock.newCondition();
private final Condition lock_full = lock.newCondition();
public void push(int v) {
lock.lock();
while (size >= _data.length) {
try {
lock_full.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
_data[size] = v;
++size;
lock_empty.signalAll();
lock.unlock();
}
public int pop() {
int v;
lock.lock();
while (size == 0) {
try {
lock_empty.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
v = _data[size - 1];
--size;
lock_full.signalAll();
lock.unlock();
return v;
}
}
???????????Condition lock????Щ?????????????飬????????????????????????Ч???
???????????Linux????????pthread_cond_wait?????????????????????? ????????????????? ū???????????????????????????????Щ?????