Java源码解析阻塞队列ArrayBlockingQueue常用方法

 更新时间:2019年01月08日 09:49:09   作者:李灿辉  
今天小编就为大家分享一篇关于Java源码解析阻塞队列ArrayBlockingQueue常用方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

本文基于jdk1.8进行分析

ArrayBlockingQueue的功能简介参考https://www.jb51.net/article/154211.htm

首先看一下ArrayBlockingQueue的成员变量。如下图。最主要的成员变量是items,它是一个Object类型的数组用于保存阻塞队列中的元素。其次是takeIndex,putIndex,count,分别表示了从队列获取元素的位置,往队列里放元素的位置和队列中元素的个数。然后是lock,notEmpty和notFull三个和锁相关的成员变量。lock是一个可重入锁,而notEmpty和notFull是和lock绑定的2个Condition。对可重入锁不是很了解的同学,可以参考这篇文章https://www.jb51.net/article/154207.htm。对可重入锁的理解,是理解ArrayBlockingQueue的基础。也可以这么说,理解了可重入锁,那么在理解ArrayBlockingQueue就很顺利了。

  /** The queued items **/
  final Object[] items;
  /** items index for next take, poll, peek or remove **/
  int takeIndex;
  /** items index for next put, offer, or add **/
  int putIndex;
  /** Number of elements in the queue **/
  int count;
  /**
   * Concurrency control uses the classic two-condition algorithm
   * found in any textbook.
   **/
  /** Main lock guarding all access **/
  final ReentrantLock lock;
  /** Condition for waiting takes **/
  private final Condition notEmpty;
  /** Condition for waiting puts **/
  private final Condition notFull;
  /**
   * Shared state for currently active iterators, or null if there
   * are known not to be any. Allows queue operations to update
   * iterator state.
   **/
  transient Itrs itrs = null;

接下来介绍ArrayBlockingQueue的主要方法。首先是入队方法。ArrayBlockingQueue的入队方法有好几个,功能略有差异,下面我们逐一介绍各个入队方法。首先看一下put方法,如下图。put方法的功能是,往队列尾部插入指定元素,如果队列已满,那么就等待可用空间。方法的实现过程是,首先判断元素是否非空。然后,进行加锁,加锁后判断队列是否已满。如果已满,则等待不满条件。当被唤醒后,进行入队操作。入队方法中,会唤醒在notEmpty条件上等待的线程。

  /**
   * Inserts the specified element at the tail of this queue, waiting
   * for space to become available if the queue is full.
   * @throws InterruptedException {@inheritDoc}
   * @throws NullPointerException {@inheritDoc}
   **/
  public void put(E e) throws InterruptedException {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      while (count == items.length)
        notFull.await();
      enqueue(e);
    } finally {
      lock.unlock();
    }
  }
  /**
   * Inserts element at current put position, advances, and signals.
   * Call only when holding lock.
   **/
  private void enqueue(E x) {
    // assert lock.getHoldCount() == 1;
    // assert items[putIndex] == null;
    final Object[] items = this.items;
    items[putIndex] = x;
    if (++putIndex == items.length)
      putIndex = 0;
    count++;
    notEmpty.signal();
  }

另一个入队方法是offer,代码如下。这个方法与add方法的区别是,offer方法是立刻返回的,它并不像add方法那样,当队列满时会一直等待。

  /**
   * Inserts the specified element at the tail of this queue if it is
   * possible to do so immediately without exceeding the queue's capacity,
   * returning {@code true} upon success and {@code false} if this queue
   * is full. This method is generally preferable to method {@link #add},
   * which can fail to insert an element only by throwing an exception.
   * @throws NullPointerException if the specified element is null
   **/
  public boolean offer(E e) {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
      if (count == items.length)
        return false;
      else {
        enqueue(e);
        return true;
      }
    } finally {
      lock.unlock();
    }
  }

接下来看一下出队方法take,代码如下。首先对可重入锁加锁,然后判断元素个数是否为0.如果为0,则等待不空条件,否则进行出队操作。

  public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      while (count == 0)
        notEmpty.await();
      return dequeue();
    } finally {
      lock.unlock();
    }
  }

ArrayListBlockingqueue中还有其他相关方法,这里就不一一介绍了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • java计算时间差的方法

    java计算时间差的方法

    这篇文章主要介绍了java计算时间差的方法,涉及java针对时间的转换与计算相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 用SpringMVC编写一个HelloWorld的详细过程

    用SpringMVC编写一个HelloWorld的详细过程

    SpringMVC是Spring的一个后续产品,是Spring的一个子项目<BR>SpringMVC 是 Spring 为表述层开发提供的一整套完备的解决方案,本文我们将用SpringMVC编写一个HelloWorld,文中有详细的编写过程,需要的朋友可以参考下
    2023-08-08
  • Retrofit+RxJava实现带进度条的文件下载

    Retrofit+RxJava实现带进度条的文件下载

    这篇文章主要为大家详细介绍了Retrofit+RxJava实现带进度条的文件下载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Java8中的类型注解浅析

    Java8中的类型注解浅析

    这篇文章主要介绍了Java8中的类型注解浅析,java8之前,注解只能是在声明的地方所使用,java8里面的注解则可以应用在任何地方,需要的朋友可以参考下
    2014-06-06
  • spring boot项目中MongoDB的使用方法

    spring boot项目中MongoDB的使用方法

    前段时间分享了关于Spring Boot中使用Redis的文章,除了Redis之后,我们在互联网产品中还经常会用到另外一款著名的NoSQL数据库MongoDB。下面这篇文章主要给大家介绍了关于在spring boot项目中MongoDB的使用方法,需要的朋友可以参考下。
    2017-09-09
  • java 在Jetty9中使用HttpSessionListener和Filter

    java 在Jetty9中使用HttpSessionListener和Filter

    这篇文章主要介绍了java 在Jetty9中使用HttpSessionListener和Filter的相关资料,需要的朋友可以参考下
    2017-06-06
  • Java 二叉树遍历的常用方法

    Java 二叉树遍历的常用方法

    二叉树的遍历可以说是解决二叉树问题的基础。我们常用的遍历方式无外乎就四种 前序遍历、中序遍历、后续遍历、层次遍历 这四种。
    2021-05-05
  • springboot 无法自动装配的问题

    springboot 无法自动装配的问题

    这篇文章主要介绍了springboot 无法自动装配的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 详解java装饰模式(Decorator Pattern)

    详解java装饰模式(Decorator Pattern)

    这篇文章主要为大家详细介绍了java装饰模式Decorator Pattern,这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装,对装饰器模式感兴趣的小伙伴们可以参考一下
    2016-04-04
  • springboot实现对注解的切面案例

    springboot实现对注解的切面案例

    这篇文章主要介绍了springboot实现对注解的切面过程,首先定义一个注解、再编写对注解的切面只是记录的执行时间和打印方法,可以实现其他逻辑,需要的朋友可以参考一下
    2022-01-01

最新评论