当前位置:主页 > java教程 > Java源码解析阻塞队列ArrayBlockingQueue功能简介

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

发布:2020-02-03 21:26:18 132


为找教程的网友们整理了Java相关的编程文章,网友李正卿根据主题投稿了本篇教程内容,涉及到Java、阻塞队列、ArrayBlockingQueue、Java源码解析阻塞队列ArrayBlockingQueue功能简介相关内容,已被580网友关注,下面的电子资料对本篇知识点有更加详尽的解释。

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 "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.
 * <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
 **/

总结

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


参考资料

相关文章

  • JavaScript中this关键字的使用方法

    发布:2020-03-12

    this是函数内部的对象并且被用于调用该函数,this在全局中的使用非常之灵活,下面就带大家来详解JavaScript中this关键字的用法


  • java解决分布式环境中高并发环境下数据插入重复问题

    发布:2019-06-28

    这篇文章主要介绍了java解决并发数据重复问题 ,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧


  • Java实现动态获取文件的绝对路径

    发布:2023-03-27

    我们知道在 Java 中读取一些配置文件信息,是在开发中十分常用的要求。这篇文章就来和大家聊聊Java如何实现动态获取文件的绝对路径,感兴趣的可以了解一下


  • Java基本数据类型不需要进行创建对象的原因

    发布:2019-07-29

    今天小编就为大家分享一篇关于Java为什么基本数据类型不需要进行创建对象?,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧


  • RateLimit使用guava来做接口限流代码示例

    发布:2021-04-29

    这篇文章主要介绍了RateLimit-使用guava来做接口限流代码示例,具有一定借鉴价值,需要的朋友可以参考下


  • Java的long和bigint长度对比详解

    发布:2019-07-04

    在本篇文章里小编给大家分享的是关于Java的long和bigint长度对比和比较以及知识点总结,需要的朋友们学习下。


  • JNI实现最简单的JAVA调用C/C++实例代码讲解

    发布:2019-09-02

    这篇文章主要介绍了JNI实现最简单的JAVA调用C/C++代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


  • 深度解析Java值传递和引用传递

    发布:2020-03-14

    本文通过实例代码给大家介绍了Java中的按值传递和按引用传递的相关知识,感兴趣的朋友跟随脚本之家小编一起学习吧


网友讨论