waitWhileEmpty();
return removeAll();
}
public synchronized boolean waitUntilEmpty(long msTimeout) throws
InterruptedException {
//如果msTimeout为零,则调用waitUntilEmpty()方法
if (msTimeout == 0L) {
waitUntilEmpty(); // use other method
return true;
}
// 等待指定的时间量
long endTime = System.currentTimeMillis() + msTimeout;
long msRemaining = msTimeout;
while (!isEmpty() && (msRemaining > 0L)) {
wait(msRemaining);
msRemaining = endTime - System.currentTimeMillis();
}
// 可能超时,也可能满足了条件,计算返回值
return isEmpty();
}
//此方法用于判断单元中的内容是否为空
public synchronized void waitUntilEmpty() throws InterruptedException {
while (!isEmpty()) {
wait();
}
}
//此方法用于判断数组中的内容是否为空,如果为空则等待
public synchronized void waitWhileEmpty() throws InterruptedException {
while (isEmpty()) {
wait();
}
}
public synchronized void waitUntilFull() throws InterruptedException {
while (!isFull()) {
wait();
}
}
//此方法判断数组中的内容是否已满,如果已满的话则让其他线程等待
public synchronized void waitWhileFull() throws InterruptedException {
while (isFull()) {
wait();
}
}
}
ObjectFIFOTest.java
public class ObjectFIFOTest
extends Object {
private static void fullCheck(ObjectFIFO fifo) {
try {
//同步化方法允许条件仍然为true时打印信息
synchronized (fifo) {
while (true) {
//调用ObjectFIFO中的waitUntilFull()方法用于判断单元中的内容是否已满
fifo.waitUntilFull();