java阻塞队列作用_介绍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

* head of the queue is that element that has been on the

* queue the longest time. The tail 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.

*

This is a classic "bounded buffer", 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.

*

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.

*

This class and its iterator implement all of the

* optional methods of the {@link Collection} and {@link

* Iterator} interfaces.

*

This class is a member of the

*

* Java Collections Framework.

* @since 1.5

* @author Doug Lea

* @param the type of elements held in this collection

**/

总结

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

以上就是本次给大家分享的关于java的全部知识点内容总结,大家还可以在下方相关文章里找到相关文章进一步学习,感谢大家的阅读和支持。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值