Java源碼解析阻塞隊(duì)列ArrayBlockingQueue功能簡(jiǎn)介
本文基于jdk1.8進(jìn)行分析。
阻塞隊(duì)列是java開發(fā)時(shí)常用的一個(gè)數(shù)據(jù)結(jié)構(gòu)。首先看一下阻塞隊(duì)列的作用是什么。阻塞隊(duì)列的作用,從源碼中類的注釋中來(lái)了解,是最清晰準(zhǔn)確的。
ArrayBlockingQueue是一個(gè)用數(shù)組實(shí)現(xiàn)的有界阻塞隊(duì)列。提供FIFO的功能。隊(duì)列頭上的元素是在隊(duì)列中呆了最長(zhǎng)時(shí)間的元素,隊(duì)列尾上的元素是在隊(duì)列中呆了時(shí)間最短的元素。新元素會(huì)插入在隊(duì)列尾部,從隊(duì)列獲取元素時(shí)會(huì)從隊(duì)列頭上獲取。
這是一個(gè)傳統(tǒng)的有界隊(duì)列,在這個(gè)有界隊(duì)列里,一個(gè)固定大小的數(shù)組用來(lái)保存生產(chǎn)者產(chǎn)生的元素和消費(fèi)者獲取的元素。一旦創(chuàng)建,大小不可改變。往已滿的隊(duì)列中嘗試添加元素,會(huì)阻塞操作。從空的隊(duì)列中獲取元素,也會(huì)阻塞操作。
這個(gè)類為等待中的生產(chǎn)著和消費(fèi)者線程排序提供可選的公平策略。默認(rèn)情況下,順序是沒(méi)有保證的。但是,一個(gè)用fairness=true創(chuàng)建的隊(duì)列可以保證FIFO特性。公平性通常會(huì)降低吞吐量,但是可以減少易變性并避免饑餓。
/**
* 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
**/
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- java ArrayBlockingQueue阻塞隊(duì)列的實(shí)現(xiàn)示例
- Java中ArrayBlockingQueue和LinkedBlockingQueue
- Java 并發(fā)編程ArrayBlockingQueue的實(shí)現(xiàn)
- java ArrayBlockingQueue的方法及缺點(diǎn)分析
- Java源碼解析阻塞隊(duì)列ArrayBlockingQueue介紹
- Java源碼解析阻塞隊(duì)列ArrayBlockingQueue常用方法
- 詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
- java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
- Java并發(fā)編程ArrayBlockingQueue的使用
相關(guān)文章
Java PDF 添加數(shù)字簽名的實(shí)現(xiàn)方法
這篇文章主要介紹了Java PDF 添加數(shù)字簽名的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Dwr3.0純注解(純Java Code配置)配置與應(yīng)用淺析三之后端反向調(diào)用前端
Dwr是為人所熟知的前端框架,其異步推送功能是為人所津津樂(lè)道的,下來(lái)主要研究一下它的這個(gè)功能是怎么應(yīng)用的;2016-04-04
java提取json中某個(gè)數(shù)組的所有值方法
下面小編就為大家分享一篇java提取json中某個(gè)數(shù)組的所有值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
springboot整合Mybatis-plus的實(shí)現(xiàn)
這篇文章主要介紹了springboot整合Mybatis-plus的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Feign 集成 Hystrix實(shí)現(xiàn)不同的調(diào)用接口不同的設(shè)置方式
這篇文章主要介紹了Feign 集成 Hystrix實(shí)現(xiàn)不同的調(diào)用接口不同的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
一篇文章帶你理解Java Spring三級(jí)緩存和循環(huán)依賴
這篇文章主要介紹了淺談Spring 解決循環(huán)依賴必須要三級(jí)緩存嗎,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09
解決@CachePut設(shè)置的key值無(wú)法與@CacheValue的值匹配問(wèn)題
這篇文章主要介紹了解決@CachePut設(shè)置的key的值無(wú)法與@CacheValue的值匹配問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
為spring get請(qǐng)求添加自定義的參數(shù)處理操作(如下劃線轉(zhuǎn)駝峰)
這篇文章主要介紹了為spring get請(qǐng)求添加自定義的參數(shù)處理操作(如下劃線轉(zhuǎn)駝峰),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

