??????.???????е???????
????????????????????к??????????г?????????????????????????е??????????????ArrayBlockingQueue???????????????????????????ArrayBlockingQueue???Щ???????????·???????????????????????в????????????е???????
????????????ArrayBlockingQueue???е?????????????
public class ArrayBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>?? java.io.Serializable {
private static final long serialVersionUID = -817911632652898426L;
/** The queued items  */
private final E[] items;
/** items index for next take?? poll or remove */
private int takeIndex;
/** items index for next put?? offer?? or add. */
private int putIndex;
/** Number of items in the queue */
private int count;
/*
* Concurrency control uses the classic two-condition algorithm
* found in any textbook.
*/
/** Main lock guarding all access */
private final ReentrantLock lock;
/** Condition for waiting takes */
private final Condition notEmpty;
/** Condition for waiting puts */
private final Condition notFull;
}
?????????????ArrayBlockingQueue???????洢?????????????????飬takeIndex??putIndex??????????????β?????±?count??????????????????
????lock???????????????notEmpty??notFull??????????
???????濴???ArrayBlockingQueue?????????????????????????汾??
public ArrayBlockingQueue(int capacity) {
}
public ArrayBlockingQueue(int capacity?? boolean fair) {
}
public ArrayBlockingQueue(int capacity?? boolean fair??
Collection<? extends E> c) {
}
???????????????????????????????????????????????????????????????????????????????????????????????????????????????????г??????
????????????????????????????put()??take()??
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
final E[] items = this.items;
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (count == items.length)
notFull.await();
} catch (InterruptedException ie) {
notFull.signal(); // propagate to non-interrupted thread
throw ie;
}
insert(e);
} finally {
lock.unlock();
}
}
??????put?????????????????????????????????????????ж?????????ж????????????????????????????????????notFull.await()???е????????????ж????????????????????
????????????????????????insert(e)???????????????????
????????????insert??????????
private void insert(E x) {
items[putIndex] = x;
putIndex = inc(putIndex);
++count;
notEmpty.signal();
}
???????????private?????????????????notEmpty???????????????????
??????????take()??????????
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
try {
while (count == 0)
notEmpty.await();
} catch (InterruptedException ie) {
notEmpty.signal(); // propagate to non-interrupted thread
throw ie;
}
E x = extract();
return x;
} finally {
lock.unlock();
}
}
??????put??????????????????put???????????notFull??????take???????????notEmpty??????take?????У??????????????????extract?????????????????extract??????????
????private E extract() {
????final E[] items = this.items;
????E x = items[takeIndex];
????items[takeIndex] = null;
????takeIndex = inc(takeIndex);
????--count;
????notFull.signal();
????return x;
????}
??????insert????????????
???????????????????????????????е?????????????????????Object.wait()??Object.notify()????????????????????-????????·????????????????Щ??????????????????????????