這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Java中怎么使用ArrayBlockingQueue解析阻塞隊(duì)列,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元市南做網(wǎng)站,已為上家服務(wù),為市南各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
阻塞隊(duì)列是java開(kāi)發(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 * 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中怎么使用ArrayBlockingQueue解析阻塞隊(duì)列了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。