阻塞队列的简单实现

/** 
 * @author luxiangxing 
 * @time   2017-05-06 
 * @email  [email protected] 
 * @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);
	}
}

猜你喜欢

转载自xiangxingchina.iteye.com/blog/2372987
今日推荐