阻塞队列 package thread.blockqueue;import java.util.LinkedList;import java.util.List;/** * 阻塞队列 * * @author Administrator * */public class BlockingQueue {// 构建队列private List queue = new LinkedList(); // 队列容量 priv
package thread.blockqueue; import java.util.LinkedList; import java.util.List; /** * 阻塞队列 * * @author Administrator * */ public class BlockingQueue{ // 构建队列 private List queue = new LinkedList<>(); // 队列容量 private int limit = 10; public BlockingQueue(int limit) { this.limit = limit; } /** * 进队列 * * @param item * @throws InterruptedException */ public synchronized void enqueue(E item) throws InterruptedException { while (this.queue.size() == this.limit) wait(); if (this.queue.size() == 0) notifyAll(); this.queue.add(item); } /** * 出队列 * * @return * @throws InterruptedException */ public synchronized E dequeue() throws InterruptedException { while (this.queue.size() == 0) wait(); if (this.queue.size() == limit) { notifyAll(); } return this.queue.remove(0); } }