把上面的內容結合起來
public class SyncQueue {
public SyncQueue(int size) {
_array = new Object[size];
_size = size;
_oldest =
_next =
}
public synchronized void put(Object o) {
while (full()) {
try {
wait();
} catch (InterruptedException ex) {
throw new ExceptionAdapter(ex);
}
}
_array[_next] = o;
_next = (_next +
notify();
}
public synchronized Object get() {
while (empty()) {
try {
wait();
} catch (InterruptedException ex) {
throw new ExceptionAdapter(ex);
}
}
Object ret = _array[_oldest];
_oldest = (_oldest +
notify();
return ret;
}
protected boolean empty() {
return _next == _oldest;
}
protected boolean full() {
return (_next +
}
protected Object [] _array;
protected int _next;
protected int _oldest;
protected int _size;
}
可以注意一下get和put方法中while的使用
[
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27222.html