public class Test1 {
static class BlockingQueue{
//普通队列实现方式两种:1.基于链表2.基于数组
private int[] array = new int[1000];
//head和tail构成一个前闭后开区间 当两者重合的时候,可能表示队列空或者满
private int head = 0;
private int tail = 0;
private int size = 0;
//队列的基本操作:1.入队列2.出队列3.取队首元素
//对于阻塞队列来说只之前入队列和出队列
//入阻塞队列
public void put(int value) throws InterruptedException {
synchronized (this) {
if (size == array.length){
wait();
}
//把value放在队尾即可
array[tail] = value;
tail++;
if (tail == array.length){
tail = 0;
}
size++;
notify();
}
}
//出阻塞队列
public int take() throws InterruptedException {
int ret = array[head];
synchronized (this) {
if (size == 0){
wait();
}
if (head == array.length){
head = 0;
}
size--;
notify();
}
return ret;
}
}
}