当前位置: 首页 > 编程笔记 >

Java源码解析阻塞队列ArrayBlockingQueue功能简介

云胤
2023-03-14
本文向大家介绍Java源码解析阻塞队列ArrayBlockingQueue功能简介,包括了Java源码解析阻塞队列ArrayBlockingQueue功能简介的使用技巧和注意事项,需要的朋友参考一下

本文基于jdk1.8进行分析

阻塞队列是java开发时常用的一个数据结构。首先看一下阻塞队列的作用是什么。阻塞队列的作用,从源码中类的注释中来了解,是最清晰准确的。

ArrayBlockingQueue是一个用数组实现的有界阻塞队列。提供FIFO的功能。队列头上的元素是在队列中呆了最长时间的元素,队列尾上的元素是在队列中呆了时间最短的元素。新元素会插入在队列尾部,从队列获取元素时会从队列头上获取。

这是一个传统的有界队列,在这个有界队列里,一个固定大小的数组用来保存生产者产生的元素和消费者获取的元素。一旦创建,大小不可改变。往已满的队列中尝试添加元素,会阻塞操作。从空的队列中获取元素,也会阻塞操作。

这个类为等待中的生产着和消费者线程排序提供可选的公平策略。默认情况下,顺序是没有保证的。但是,一个用fairness=true创建的队列可以保证FIFO特性。公平性通常会降低吞吐量,但是可以减少易变性并避免饥饿。

/**
 * A bounded {@linkplain BlockingQueue blocking queue} backed by an
 * array. This queue orders elements FIFO (first-in-first-out). The
 * <em>head</em> of the queue is that element that has been on the
 * queue the longest time. The <em>tail</em> of the queue is that
 * element that has been on the queue the shortest time. New elements
 * are inserted at the tail of the queue, and the queue retrieval
 * operations obtain elements at the head of the queue.
 * <p>This is a classic &quot;bounded buffer&quot;, in which a
 * fixed-sized array holds elements inserted by producers and
 * extracted by consumers. Once created, the capacity cannot be
 * changed. Attempts to {@code put} an element into a full queue
 * will result in the operation blocking; attempts to {@code take} an
 * element from an empty queue will similarly block.
 * <p>This class supports an optional fairness policy for ordering
 * waiting producer and consumer threads. By default, this ordering
 * is not guaranteed. However, a queue constructed with fairness set
 * to {@code true} grants threads access in FIFO order. Fairness
 * generally decreases throughput but reduces variability and avoids
 * starvation.
 * <p>This class and its iterator implement all of the
 * <em>optional</em> methods of the {@link Collection} and {@link
 * Iterator} interfaces.
 * <p>This class is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html" rel="external nofollow" >
 * Java Collections Framework</a>.
 * @since 1.5
 * @author Doug Lea
 * @param <E> the type of elements held in this collection
 **/

总结

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

 类似资料:
  • 本文向大家介绍Java源码解析阻塞队列ArrayBlockingQueue介绍,包括了Java源码解析阻塞队列ArrayBlockingQueue介绍的使用技巧和注意事项,需要的朋友参考一下 Java的阻塞队列,在实现时,使用到了lock和condition,下面是对其主要方法的介绍。 首先看一下,阻塞队列中使用到的锁。 主要的锁是一个可重入锁,根据注释,它是用来保证所有访问的同步。此外,还有2个

  • 主要内容:1 LinkedBlockingDeque的概述,2 LinkedBlockingDeque的原理,2.1 主要属性,2.2 构造器,2.3 入队操作,2.4 出队操作,2.5 检查操作,2.6 size操作,2.7 迭代操作,3 LinkedBlockingDeque的总结基于JDK1.8详细介绍了LinkedBlockingDeque的底层源码实现,包括双端队列的入队列、出队列、迭代等操作源码。实际上LinkedBlockingDeque的源码还是非常简单的! 1 LinkedBl

  • 主要内容:1 LinkedTransferQueue的概述,2 LinkedTransferQueue的原理,2.1 主要属性,2.2 构造器,2.3 xfer核心方法,2.4 入队操作,2.5 出队操作,2.6 传递操作,2.7 检查操作,2.8 size操作,2.9 迭代操作,2.10 执行流程,3 LinkedTransferQueue的总结基于JDK1.8详细介绍了LinkedTransferQueue的底层源码实现,包括入队、出队、传递等操作源码,以及相比于LinkedBlocking

  • 主要内容:1 DelayQueue的概述,2 DelayQueue的原理,2.1 主要属性,2.2 构造器,2.3 入队操作,2.4 出队操作,2.5 检查操作,2.6 size操作,2.7 迭代操作,3 DelayQueue的应用,3.1 案例,4 DelayQueue的总结基于JDK1.8详细介绍了DelayQueue的底层源码实现,包括延迟出队的原理,以及入队列、出队列等操作的源码。 1 DelayQueue的概述 public class DelayQueue< E extends De

  • 主要内容:1 PriorityBlockingQueue的概述,2 PriorityBlockingQueue的原理,2.1 主要属性,2.2 构造器,2.3 入队操作,2.4 出队操作,2.5 检查操作,2.6 size操作,2.7 迭代操作,3 PriorityBlockingQueue的案例,3.1 基本使用,3.2 优先任务队列,4 PriorityBlockingQueue的总结基于JDK1.8详细介绍了PriorityBlockingQueue的底层源码实现,包括小顶堆和优先级排序的

  • 主要内容:1 ConcurrentLinkedQueue的概述,2 ConcurrentLinkedQueue的实现,2.1 基本结构,2.2 构造器,2.3 入队操作,2.4 出队操作,2.5 过程详解,2.6 获取操作,2.7 其他操作,3 ConcurrentLinkedQueue的总结基于JDK1.8详细介绍了ConcurrentLinkedQueue的底层源码实现,包括同步原理、入队操作、出队操作、获取操作等。 1 ConcurrentLinkedQueue的概述 public cla