實現 BlockingQueue 的 take() 和 put()
public class BlockingQueueImpl<T> implements BlockingQueue{
Queue<T> q = new LinkedList<T>();
int cap = 10;
long timeOut = 1000;
@Override
public Object take() {
while(q.isEmpty()){
try {
this.wait(timeOut);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object rst = q.poll();
this.notifyAll();
return rst;
}
@Override
public void put(Object obj) {
while(q.size()==cap){
try{
this.wait(timeOut);
} catch (InterruptedException e){
e.printStackTrace();
}
}
q.offer((T) obj);
}
}