return (size == 0);
}
//判断是否数组已满
public synchronized boolean isFull() {
return (size == capacity);
}
//向数组中添加数据
public synchronized void add(Object obj) throws InterruptedException {
//调用waitWhileFull()方法
waitWhileFull();
//想数组中添加内容
queue[head] = obj;
//修改要添加的位置
head = (head + 1) % capacity;
//数组的长度加1
size++;
//唤醒其他线程
notifyAll();
}
//加入一个数组
public synchronized void addEach(Object[] list) throws InterruptedException {
//在这里用for循环将传入的数组内容依次加入到对象数组之中
for (int i = 0; i < list.length; i++) {
add(list[i]);
}
}
//从数组中移出内容
public synchronized Object remove() throws InterruptedException {
//调用waitWhileEmpty()方法
waitWhileEmpty();
//从对象数组之中取出内容
Object obj = queue[tail];
//取出内容后将此数组的内容请空
queue[tail] = null;
//修改删除位置
tail = (tail + 1) % capacity;
//长度减1
size--;
//唤醒其他等待的线程序继续执行
notifyAll();
//返回取出的对象
return obj;
}
//此方法用于移出所有内容
public synchronized Object[] removeAll() throws InterruptedException {
//使用当前的长度初始化一个对象数组
Object[] list = new Object[size];
//用for循环调用remove()方法
for (int i = 0; i < list.length; i++) {
list[i] = remove();
}
return list;
}
//等待数组中至少只有一个项之后删除该项
public synchronized Object[] removeAtLeastOne() throws InterruptedException {
//调用waitWhileEmpty方法用于判断内容是否为空