一个简单的阻塞队列实现

闲话不说,直接 上代码。
/**
 * 一个简单的阻塞队列
 * 
 * @author Kevin
 * 
 * @param <E>
 */

class SimpleBlockingQueue<E> {
	/**
	 * 默认的队列容量
	 */
	private static final int DEFAULT_CAPACITY = 128;

	/* 存储队列元素的数组 */
	private E[] queue;

	/* 队列初始容量 */
	private int capacity;

	/* 队列中元素个数 */
	private int size;

	/* 队头索引 */
	private int head;

	/* 队尾索引 */
	private int tail;

	/* 显示锁 */
	private Lock lock = new ReentrantLock();

	/* 条件谓词:size<capacity */
	private Condition notFull = lock.newCondition();

	/* 条件谓词:size>0 */
	private Condition notEmpty = lock.newCondition();

	public SimpleBlockingQueue() {
		this(DEFAULT_CAPACITY);
	}

	public SimpleBlockingQueue(int capacity) {
		this.capacity = capacity;
		queue = (E[]) new Object[capacity];
	}

	/**
	 * 用传递的集合的元素作为队列 的初始元素
	 * 
	 * @param c
	 * @param capacity
	 */
	public SimpleBlockingQueue(Collection<E> c, int capacity) {
		this(capacity);

		// 如果传入的初始容量小于集合的大小 ,则抛出异常
		if (capacity < c.size()) {
			throw new IllegalArgumentException("");
		}

		Iterator<E> iter = c.iterator();
		while (iter.hasNext()) {
			queue[tail++] = iter.next();
			++size;
		}
	}

	/**
	 * 添加一个元素到队尾,如果队列已满,该方法将阻塞
	 * @param element
	 * @throws InterruptedException
	 */
	public void put(E element) throws InterruptedException {
		lock.lock();
		try {
			while (size >= capacity) {
				notFull.await();
			}
			queue[tail] = element;
			if (++tail == capacity) {
				tail = 0;
			}
			++size;
			notEmpty.signal();

		} finally {
			lock.unlock();
		}
	}

	/**
	 * 从队头移除一个元素,并返回该元素,如果队列 为空,将阻塞直到有元素 为止
	 * @return
	 * @throws InterruptedException
	 */
	public E take() throws InterruptedException {
		lock.lock();
		try {
			while (size == 0) {
				notEmpty.await();
			}
			E value = queue[head];
			queue[head] = null;
			if (++head == capacity) {
				head = 0;
			}
			--size;
			notFull.signal();
			return value;
		} finally {
			lock.unlock();
		}
	}
}


闲来无事,随手写的一个简单实现,如有错误,还请斧正。。~~

下面是一个基于 Java 的简单 **阻塞队列(Blocking Queue)** 实现。这个队列使用 `synchronized` 关键字来实现线程安全,并通过 `wait()` 和 `notify()` 方法实现阻塞功能。 ```java import java.util.LinkedList; import java.util.Queue; public class MyBlockingQueue<T> { private Queue<T> queue; private int maxSize; public MyBlockingQueue(int maxSize) { this.queue = new LinkedList<>(); this.maxSize = maxSize; } // 入队方法(如果队列已满,则阻塞) public synchronized void put(T item) throws InterruptedException { while (queue.size() == maxSize) { wait(); // 队列满时等待 } queue.add(item); notify(); // 通知可能正在等待的消费者 } // 出队方法(如果队列为空,则阻塞) public synchronized T take() throws InterruptedException { while (queue.isEmpty()) { wait(); // 队列空时等待 } T item = queue.poll(); notify(); // 通知可能正在等待的生产者 return item; } // 获取当前队列大小 public synchronized int size() { return queue.size(); } // 检查队列是否为空 public synchronized boolean isEmpty() { return queue.isEmpty(); } // 检查队列是否已满 public synchronized boolean isFull() { return queue.size() == maxSize; } } ``` ### 示例用法 ```java public class BlockingQueueTest { public static void main(String[] args) { MyBlockingQueue<Integer> queue = new MyBlockingQueue<>(5); // 生产者线程 new Thread(() -> { int i = 0; while (true) { try { queue.put(i); System.out.println("Produced: " + i++); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); // 消费者线程 new Thread(() -> { while (true) { try { Integer value = queue.take(); System.out.println("Consumed: " + value); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } } ``` ### 功能说明: - `put()` 方法在队列满时会阻塞,直到有空间。 - `take()` 方法在队列空时会阻塞,直到有元素可取。 - 使用 `synchronized` 来保证线程安全。 - 使用 `wait()` 和 `notify()` 实现线程间通信。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值