您好,登錄后才能下訂單哦!
這期內(nèi)容當中小編將會給大家?guī)碛嘘PJava中怎么使用ArrayBlockingQueue解析阻塞隊列,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
阻塞隊列是java開發(fā)時常用的一個數(shù)據(jù)結構。首先看一下阻塞隊列的作用是什么。阻塞隊列的作用,從源碼中類的注釋中來了解,是最清晰準確的。
ArrayBlockingQueue是一個用數(shù)組實現(xiàn)的有界阻塞隊列。提供FIFO的功能。隊列頭上的元素是在隊列中呆了最長時間的元素,隊列尾上的元素是在隊列中呆了時間最短的元素。新元素會插入在隊列尾部,從隊列獲取元素時會從隊列頭上獲取。
這是一個傳統(tǒng)的有界隊列,在這個有界隊列里,一個固定大小的數(shù)組用來保存生產(chǎn)者產(chǎn)生的元素和消費者獲取的元素。一旦創(chuàng)建,大小不可改變。往已滿的隊列中嘗試添加元素,會阻塞操作。從空的隊列中獲取元素,也會阻塞操作。
這個類為等待中的生產(chǎ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 **/
上述就是小編為大家分享的Java中怎么使用ArrayBlockingQueue解析阻塞隊列了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。