使用 ReentrantLock 和 Condition 实现一个阻塞队列

前言

从之前的阻塞队列的源码分析中,我们知道,JDK 中的阻塞队列是使用 ReentrantLock 和 Condition 实现了,我们今天来个简易版的。代码如下:

代码

public class BoundedBuffer {

  final ReentrantLock lock = new ReentrantLock();
  final ConditionObject notFull = (ConditionObject) lock.newCondition();
  final ConditionObject notEmpty = (ConditionObject) lock.newCondition();

  final Object[] items = new Object[100];
  int putptr, takeptr, count;

  public void put(Object x) throws InterruptedException {
    lock.lock();
    try {
      // 当数组满了
      while (count == items.length) {
        // 释放锁,等待
        notFull.await();
      }
      // 放入数据
      items[putptr] = x;
      // 如果到最后一个位置了,下标从头开始,防止下标越界
      if (++putptr == items.length) {
        // 从头开始
        putptr = 0;
      }
      // 对 count ++ 加加
      ++count;
      // 通知 take 线程,可以取数据了,不必继续阻塞
      notEmpty.signal();
    } finally {
      lock.unlock();
    }
  }

  public Object take() throws InterruptedException {
    lock.lock();
    try {
      // 如果数组没有数据,则等待
      while (count == 0) {
        notEmpty.await();
      }
      // 取数据
      Object x = items[takeptr];
      // 如果到数组尽头了,就从头开始
      if (++takeptr == items.length) {
        // 从头开始
        takeptr = 0;
      }
      // 将数量减1
      --count;
      // 通知阻塞的 put 线程可以装填数据了
      notFull.signal();
      return x;
    } finally {
      lock.unlock();
    }
  }
复制代码

其实,这并不是我写的,而是 Condition 接口的 JavaDoc 文档中写的。并且文档中说,请不要再次实现这个队列,因为 JDK 内部已经是实现了。原话如下:

(The {@link java.util.concurrent.ArrayBlockingQueue} class provides this functionality, so there is no reason to implement this sample usage class.)

楼主只是觉得这个代码写的挺好的,所以分享一下,其中关键的还是 Condition 和重入锁,重入锁的核心源码我们已经看过了,今天来看看 Condition 的源码。所以我们今天使用了这个代码作为一个入口。

可以看到,Condition 的重要方法就是 await 和 signal,类似 Object 类的 wait 和 notify 方法。

我们后面会详细讲讲这两个方法的实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值