}
queue[head] = obj;
//修改位置指针
/*
head += 1 ;
if(head>=5)
head = 0 ;
*/
head = (head + 1) % capacity;
//内容的容量加1
size++;
notifyAll(); //唤醒其他线程继续执行
}
//从数组中移除对象
public synchronized Object remove() throws InterruptedException {
//如果当前的容量size为零,表示没有内容,则线程等待
while (size == 0) {
wait();
}
//否则执行此处
//从对象数组中取出数据
Object obj = queue[tail];
//修改里面的内容让其等于null
queue[tail] = null;
//修改删除的位置
tail = (tail + 1) % capacity;
//容量减1
size--;
//唤醒其他线程
notifyAll();
//返回取出的对象
return obj;
}
//此方法用于打印
public synchronized void printState() {
StringBuffer sb = new StringBuffer();
sb.append("SimpleObjectFIFO:\n");
sb.append(" capacity=" + capacity + "\n");
sb.append(" size=" + size);
if (isFull()) {
sb.append(" - FULL");
}
else if (size == 0) {
sb.append(" - EMPTY");
}
sb.append("\n");
sb.append(" head=" + head + "\n");
sb.append(" tail=" + tail + "\n");
for (int i = 0; i < queue.length; i++) {
sb.append(" queue[" + i + "]=" + queue[i] + "\n");
}