目录
只有一个生产者一个消费者时:
public class MyArrayBlockingQueue {
private long[] array;
private int frontIndex;//永远在队列的第一个元素位置
private int rearIndex;//永远在队列的最后一个元素位置的下一个位置
private int size;
public MyArrayBlockingQueue(int capacity) {
array = new long[capacity];
frontIndex = 0;
rearIndex = 0;
size = 0;
}
/**
* 向阻塞队列中添加元素
*
* @param e
*/
public synchronized void put(long e) throws InterruptedException {
//判断队列是否已满
// if (array.length == size) {
// //队列已满 等待
// this.wait();
//
// }
while (array.length==size){
this.wait();
}
//预期:队列一定不是满的 考虑到wait 有一种被唤醒的条件是假唤醒,假唤醒的队列是满的