PriorityBlockingQueue

整體描述

An unbounded blocking queue that uses the same ordering rules as class PriorityQueue and supplies blocking retrieval operations.

注意

  1. While this queue is logically unbounded, attempted additions may fail due to resource exhaustion (causing OutOfMemoryError).
  2. This class does not permit null elements.
  3. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so results in ClassCastException).

迭代
This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces.

The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityBlockingQueue in any particular order.
If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

Also, method drainTo can be used to remove some or all elements in priority order and place them in another collection.

不保證順序病蛉,在優(yōu)先級(jí)相同的情況下
Operations on this class make no guarantees about the **ordering of elements with equal priority. **

強(qiáng)制有序
If you need to enforce an ordering, you can define custom classes or comparators that use a secondary key to break ties in primary priority values.

舉個(gè)例子
For example, here is a class that applies first-in-first-out tie-breaking to comparable elements. To use it, you would insert a new FIFOEntry(anEntry) instead of a plain entry object.

 class FIFOEntry ? super E>>
     implements Comparable  > {
   static final AtomicLong seq = new AtomicLong(0);
   final long seqNum;
   final E entry;
   public FIFOEntry(E entry) {
     seqNum = seq.getAndIncrement();
     this.entry = entry;
   
   public E getEntry() { return entry; }
   public int compareTo(FIFOEntry  other) {
     int res = entry.compareTo(other.entry);
     if (res == 0 && other.entry != this.entry)
       res = (seqNum < other.seqNum ? -1 : 1);
     return res;
   }
 }}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市句占,隨后出現(xiàn)的幾起案子纪蜒,更是在濱河造成了極大的恐慌背伴,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,865評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蟹肘,居然都是意外死亡璃吧,警方通過查閱死者的電腦和手機(jī)楣导,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,296評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來畜挨,“玉大人筒繁,你說我怎么就攤上這事“驮” “怎么了毡咏?”我有些...
    開封第一講書人閱讀 169,631評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長务冕。 經(jīng)常有香客問我血当,道長,這世上最難降的妖魔是什么禀忆? 我笑而不...
    開封第一講書人閱讀 60,199評(píng)論 1 300
  • 正文 為了忘掉前任臊旭,我火速辦了婚禮,結(jié)果婚禮上箩退,老公的妹妹穿的比我還像新娘离熏。我一直安慰自己,他們只是感情好戴涝,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,196評(píng)論 6 398
  • 文/花漫 我一把揭開白布滋戳。 她就那樣靜靜地躺著钻蔑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪奸鸯。 梳的紋絲不亂的頭發(fā)上咪笑,一...
    開封第一講書人閱讀 52,793評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音娄涩,去河邊找鬼窗怒。 笑死,一個(gè)胖子當(dāng)著我的面吹牛蓄拣,可吹牛的內(nèi)容都是我干的扬虚。 我是一名探鬼主播,決...
    沈念sama閱讀 41,221評(píng)論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼球恤,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼辜昵!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起咽斧,我...
    開封第一講書人閱讀 40,174評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤堪置,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后收厨,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體晋柱,經(jīng)...
    沈念sama閱讀 46,699評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,770評(píng)論 3 343
  • 正文 我和宋清朗相戀三年诵叁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雁竞。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,918評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡拧额,死狀恐怖碑诉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情侥锦,我是刑警寧澤进栽,帶...
    沈念sama閱讀 36,573評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站恭垦,受9級(jí)特大地震影響快毛,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜番挺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,255評(píng)論 3 336
  • 文/蒙蒙 一唠帝、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧玄柏,春花似錦襟衰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,749評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绍坝。三九已至,卻和暖如春苔悦,著一層夾襖步出監(jiān)牢的瞬間轩褐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,862評(píng)論 1 274
  • 我被黑心中介騙來泰國打工间坐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留灾挨,地道東北人邑退。 一個(gè)月前我還...
    沈念sama閱讀 49,364評(píng)論 3 379
  • 正文 我出身青樓竹宋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親地技。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蜈七,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,926評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容