阻塞队列的简单实现

阅读更多
/** 
 * @author luxiangxing 
 * @time   2017-05-06 
 * @email  [email protected] 
 * @tel    15330078427 
 */  
public class BlockingQueue {
	private List queue = new LinkedList();
	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);
	}
}

 

你可能感兴趣的:(阻塞队列的简单实现)