




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、java集合框架arrayblockingqueue應(yīng)用分析arrayblockingqueue是一個(gè)由數(shù)組支持的有界阻塞隊(duì)列。此隊(duì)列按fifo (先進(jìn)先出)原則 對(duì)元素進(jìn)行排序。隊(duì)列的頭部是在隊(duì)列中存在時(shí)間最長(zhǎng)的元素queue1. arraydeque,(數(shù)組雙端隊(duì)列)2. priorityqueue,(優(yōu)先級(jí)隊(duì)列)3. concurrentlinkedqueue,(基于鏈表的并發(fā)隊(duì)列)4. delayqueue,(延期阻塞隊(duì)列)(阻塞隊(duì)列實(shí)現(xiàn)了 blockingqueue接口)5. arrayblockingqueue,(基于數(shù)組的并發(fā)阻塞隊(duì)列)6. linkedblockingqueue
2、,(基于鏈表的 fifo 阻塞隊(duì)列)7. linkedblockingdeque,(基于鏈表的fifo雙端阻塞隊(duì)列)8. priorityblockingqueue,(帶優(yōu)先級(jí)的無界阻塞隊(duì)列) 9.synchronousqueue (并發(fā)同步阻塞隊(duì)列)arrayblockingqueue是一個(gè)由數(shù)組支持的有界阻塞隊(duì)列。此隊(duì)列按fifo (先進(jìn)先出)原則對(duì)元素進(jìn)行排序。隊(duì) 列的頭部是在隊(duì)列中存在時(shí)間最長(zhǎng)的元素。隊(duì)列的尾部是在隊(duì)列中存在時(shí)間最短的元素。 新元素插入到隊(duì)列的尾部,隊(duì)列獲取操作則是從隊(duì)列頭部開始獲得元素。這是一個(gè)典型的“有界緩存區(qū)”,固定大小的數(shù)組在其中保持生產(chǎn)者插入的元素和使用者提
3、取的元素。一旦創(chuàng)建了這樣的緩存區(qū),就不能再增加其容量。試圖向已滿隊(duì)列中放入元素 會(huì)導(dǎo)致操作受阻塞;試圖從空隊(duì)列中提取元素將導(dǎo)致類似阻塞。此類支持對(duì)等待的生產(chǎn)者線程和消費(fèi)者線程進(jìn)行排序的可選公平策略。默認(rèn)情況下,不保 證是這種排序。然而,通過將公平性(fairness)設(shè)置為true而構(gòu)造的隊(duì)列允許按照fifo 順序訪問線程。公平性通常會(huì)降低吞吐量,但也減少了可變性和避免了 “不平衡性”。復(fù)制代碼代碼如下:public class arrayblockingqueue<e> extends abstractqueue<e> implements blockingqueue
4、<e>, java.io.serializable /*隊(duì)列元素?cái)?shù)組*/private final e items;/*獲取、刪除元素時(shí)的索引(take, poll或remove操作)*/private int takeindex;/*添加元素時(shí)的索引(put, offer或add操作)*/private int putlndex;/*隊(duì)列元素的數(shù)目*/private int count;/* 鎖 */private final reentrantlock lock;/*獲取操作時(shí)的條件/private final condition notempty;/*插入操作時(shí)的條件/pri
5、vate final condition notfull;超出數(shù)組長(zhǎng)度時(shí),重設(shè)為ofinal int inc(int i) return (+i = items.length)? 0 : i;*插入元素(在獲得鎖的情況下才調(diào)用)7private void insert(e x) itemsputlndex = x; putlndex = inc(putlndex);+count;notempty.signal();/*獲取并移除元素(在獲得鎖的情況下才調(diào)用)*/private e extract() final e items = this.items;e x = itemstakelndex
6、;itemstakelndex = null;takeindex = inc(takelndex);/移到下一個(gè)位置-count;notfullsignal();return x;/*刪除i位置的元素*/void removeat(int i) final e items = this.items;/ if removing front item, just advanceif (i = takeindex) itemstakelndex = null; takeindex = inc(takelndex); else /把i后面的直到putlndex的元素都向前移動(dòng)一個(gè)位置 for (;)
7、int nexti = inc(i);if (nexti != putlndex) itemsi = itemsnexti; i = nexti;else itemsi = null; putlndex = i;break; -count;notfull.signal();*構(gòu)造方法,指定容量,默認(rèn)策略(不是按照fifo的順序訪問)*/public arrayblockingqueue(int capacity) this(capacity, false);*構(gòu)造方法,指定容量及策略*/public arrayblockingqueue(int capacity, boolean fair)
8、訐(capacity <= 0)throw new hlegalargumentexception();this.items = (e) new objectcapacity;lock = new reentrantlock(fair);notempty = lock.newcondition();notfull = lock.newcondition();*通過集合構(gòu)造 / public arrayblockingqueue(int capacity, boolean fair, collection<? extends e> c) this(capacity, fair)
9、;if (capacity < c.size()throw new ihegalargumentexception();for (lterator<? extends e> it = c.iterator(); it.hasnextf);) add(it.next();/*插入元素到隊(duì)尾(super調(diào)用offer方法)* public boolean add(e e) * if (offer(e)* return true;* else* throw new hlegalstateexception(llqueue full11);*將指定的元素插入到此隊(duì)列的尾部(如果立即可
10、行且不會(huì)超過該隊(duì)列的容量),*在成功時(shí)返回true,如果此隊(duì)列已滿,則拋出hlegalstateexceptiono*/public boolean add(e e) return super.add(e);/*將指定的元素插入到此隊(duì)列的尾部(如果立即可行且不會(huì)超過該隊(duì)列的容量),*在成功時(shí)返回true,如果此隊(duì)列已滿,則返回falseo7public boolean offer(e e) if (e = null) throw new nullpointerexception();final reentrantlock lock = this jock;lock.lock();tryif (
11、count = items .length)return false;else insert(e);return true; finally lock.unlock();/*將指定的元素插入此隊(duì)列的尾部,如果該隊(duì)列已滿,則等待可用的空間。*/public void put(e e) throws interruptedexception if (e = null) throw new nullpointerexception();final e items = this.items;final reentrantlock lock = thisck;lockocklnterruptibly()
12、;trytrywhile (count = itemsength)notfull.await(); catch (interruptedexception ie) notfullsignal(); / propagate to non-interrupted threadthrow ie;insert(e); finally!iock.unlock();/*將指定的元素插入此隊(duì)列的尾部,如果該隊(duì)列已滿,則在到達(dá)指定的等待時(shí)間之前等待 可用的空間。*/public boolean offer(e e, long timeout, timeunit unit)throws interruptede
13、xception if (e = null) throw new nullpointerexception();long nanos = unit.tonanos(timeout);final reentrantlock lock = this.lock;lock.locklnterruptiblyo;try for (;) if (count != items.length) insert(e);return true;if (nanos <= 0)/如果時(shí)間到了就返回return false;trynanos = notfull.awaitnanos(nanos); catch (i
14、nterruptedexception ie) notfull.signal(); / propagate to non-interrupted thread throw ie; finally!iock.unlock();獲取并移除此隊(duì)列的頭,如果此隊(duì)列為空,則返回nullpublic e poll() final reentrantlock lock = this.lock;lock.lock();tryif (count = 0)return null;e x = extract();return x; finally!iock.unlock();獲取并移除此隊(duì)列的頭部,在元素變得可用之
15、前一直等待(如果有必要)。public e take() throws interruptedexception final reentrantlock lock = this.lock;lockocklnterruptibly();trytrywhile (count = 0)notempty.await(); catch (interruptedexception ie) notempty.signal(); / propagate to non-interrupted threadthrow ie;e x = extract();return x; finally!iock.unlock
16、();獲取并移除此隊(duì)列的頭部,在指定的等待時(shí)間前等待可用的元素(如果有必要)。public e poll(long timeout, timellnit unit) throws interruptedexception long nanos = unit.tonanos(timeout);final reentrantlock lock = this jock;lock.locklnterruptibly();tryfor (;) if (count != 0) e x = extract();return x;if (nanos <= 0)return null;trynanos =
17、 notempty.awaitnanos(nanos); catch (interruptedexception ie) notempty.signal(); / propagate to non-interrupted threadthrow ie; finally iock.unlock();獲取但不移除此隊(duì)列的頭;如果此隊(duì)列為空,則返回nullpublic e peek() final reentrantlock lock = this.lock;lock.lock();tryreturn (count = 0) ? null: itemstakelndex; finally lock.
18、unlock();*返回此隊(duì)列中元素的數(shù)量。*/public int size() final reentrantlock lock = this.lock;lock.lock();tryreturn count; finally lock.unlock();*返回在無阻塞的理想情況下(不存在內(nèi)存或資源約束)此隊(duì)列能接受的其他元素?cái)?shù)量。*/public int remainingcapacity() final reentrantlock lock = this.lock;lock.lock();tryreturn items length count; finally!iock.unlock();*從
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 電力分包技術(shù)服務(wù)合同
- 文化創(chuàng)意行業(yè)作品征集表格
- 《物質(zhì)的基本屬性與狀態(tài):九年級(jí)科學(xué)物理教案》
- 湖北省孝感市漢川市2024-2025學(xué)年七年級(jí)上學(xué)期期末生物學(xué)試題(含答案)
- 年度團(tuán)建活動(dòng)設(shè)計(jì)與執(zhí)行方案
- 自動(dòng)售貨機(jī)銷售合同協(xié)議
- 公司內(nèi)部事務(wù)處理指南
- 城市地鐵線路建設(shè)與運(yùn)營(yíng)合同
- 企業(yè)與政府合作的環(huán)保協(xié)議
- 煤炭國(guó)際貿(mào)易合同
- 初中語文八年級(jí)上冊(cè)19《蘇州園林》公開課一等獎(jiǎng)創(chuàng)新教學(xué)設(shè)計(jì)
- 2024年山東省泰安市中考英語真題(解析版)
- 陜鼓集團(tuán)線上筆試題目
- 三年級(jí)數(shù)學(xué)下冊(cè)一兩位數(shù)乘兩位數(shù)的乘法2問題解決作業(yè)課件西師大版
- 《交通事故車輛及財(cái)物損失價(jià)格鑒證評(píng)估技術(shù)規(guī)范》
- 《基于mRNA-LNP技術(shù)的(細(xì)胞)免疫治療產(chǎn)品開發(fā)指南》征求意見稿
- LYT 2085-2013 森林火災(zāi)損失評(píng)估技術(shù)規(guī)范
- 2024兩人合伙人合作簡(jiǎn)單協(xié)議書范本
- 中國(guó)的地理實(shí)踐教學(xué)
- 《跟上兔子》繪本五年級(jí)第1季A-Magic-Card
- 建筑擋煙垂壁設(shè)計(jì)圖集
評(píng)論
0/150
提交評(píng)論