Java源碼解析阻塞隊列ArrayBlockingQueue功能簡介
本文基于jdk1.8進(jìn)行分析。
阻塞隊列是java開發(fā)時常用的一個數(shù)據(jù)結(jié)構(gòu)。首先看一下阻塞隊列的作用是什么。阻塞隊列的作用,從源碼中類的注釋中來了解,是最清晰準(zhǔn)確的。
ArrayBlockingQueue是一個用數(shù)組實現(xiàn)的有界阻塞隊列。提供FIFO的功能。隊列頭上的元素是在隊列中呆了最長時間的元素,隊列尾上的元素是在隊列中呆了時間最短的元素。新元素會插入在隊列尾部,從隊列獲取元素時會從隊列頭上獲取。
這是一個傳統(tǒng)的有界隊列,在這個有界隊列里,一個固定大小的數(shù)組用來保存生產(chǎn)者產(chǎn)生的元素和消費者獲取的元素。一旦創(chuàng)建,大小不可改變。往已滿的隊列中嘗試添加元素,會阻塞操作。從空的隊列中獲取元素,也會阻塞操作。
這個類為等待中的生產(chǎn)著和消費者線程排序提供可選的公平策略。默認(rèn)情況下,順序是沒有保證的。但是,一個用fairness=true創(chuàng)建的隊列可以保證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 **/
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- java ArrayBlockingQueue阻塞隊列的實現(xiàn)示例
- Java中ArrayBlockingQueue和LinkedBlockingQueue
- Java 并發(fā)編程ArrayBlockingQueue的實現(xiàn)
- java ArrayBlockingQueue的方法及缺點分析
- Java源碼解析阻塞隊列ArrayBlockingQueue介紹
- Java源碼解析阻塞隊列ArrayBlockingQueue常用方法
- 詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
- java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
- Java并發(fā)編程ArrayBlockingQueue的使用
相關(guān)文章
Spring中的@RestControllerAdvice注解使用方法解析
這篇文章主要介紹了Spring中的@RestControllerAdvice注解使用方法解析,@RestControllerAdvice是Controller的增強(qiáng) 常用于全局異常的捕獲處理 和請求參數(shù)的增強(qiáng),需要的朋友可以參考下2024-01-01多線程Thread,Runnable,Callable實現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Java多線程如何實現(xiàn)Thread,Runnable,Callable的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08java使用DOM對XML文檔進(jìn)行增刪改查操作實例代碼
這篇文章主要介紹了java使用DOM對XML文檔進(jìn)行增刪改查操作實例代碼,實例涉及對xml文檔的增刪改查,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02RocketMQ?ConsumeQueue與IndexFile實時更新機(jī)制源碼解析
這篇文章主要為大家介紹了RocketMQ?ConsumeQueue與IndexFile實時更新機(jī)制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05java關(guān)于String.split("|")的使用方式
這篇文章主要介紹了java關(guān)于String.split("|")的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02