/**
* @author luxiangxing
* @time 2017-05-06
* @email xiangxingchina@163.com
* @tel 15330078427
*/
public class BlockingQueue<E> {
private List<E> queue = new LinkedList<E>();
private int limit = 10;
public BlockingQueue(int limit) {
this.limit = limit;
}
public synchronized void put(E e) throws InterruptedException {
while (this.queue.size() == this.limit) {
wait();
}
if (this.queue.size() == 0) {
notifyAll();
}
this.queue.add(e);
}
public synchronized E get() throws InterruptedException {
while (this.queue.size() == 0) {
wait();
}
if (this.queue.size() == this.limit) {
notifyAll();
}
return this.queue.remove(0);
}
}