海闊天空

          I'm on my way!
          隨筆 - 17, 文章 - 69, 評論 - 21, 引用 - 0
          數據加載中……

          java中關于優先級隊列的實現

                  這幾天一直在搞關于優先級隊列的實現,因為要考慮到線程的安全,所以PriorityQueue就不適用了。一個非常簡單的實現方 法,那就是把優先級比較好的插入一個隊列,優先級低的插入另一個隊列,取數的時候先在優先級高的隊列上取數。這有個缺點就是如果優先級別越多的話,隊列就 越多。
                  因為要線程安全,隊列采用ConcurrentLinkedQueue這個線程安全的,而api文檔上說ConcurrentLinkedQueue采用了有效的“無等待 (wait-free)”算法,所以它的吞吐量是很不錯的!
              簡單代碼如下:
          1. package test;

          2. import java.util.concurrent.ConcurrentLinkedQueue;

          3. public class PriorityQueueTest {

          4.     public static void main(String[] args) {
          5.         ConcurrentLinkedQueue<String> highPriority = new ConcurrentLinkedQueue<String>(); //高優先級
          6.         ConcurrentLinkedQueue<String> lowPriority = new ConcurrentLinkedQueue<String>();  //低優先級
          7.         
          8.         highPriority.add("aaa");
          9.         highPriority.add("bbb");
          10.         highPriority.add("111");
          11.         
          12.         lowPriority.add("ccc");
          13.         lowPriority.add("ddd");
          14.         lowPriority.add("222");
          15.         
          16.         int i = 0 ,j = 0, k=0;
          17.         while(true){
          18.             while(true){
          19.                 if(!highPriority.isEmpty()){
          20.                     System.out.print(highPriority.remove());
          21.                     i++;
          22.                     k++;
          23.                     System.out.println(", i = "+i+", k="+k);
          24.                     break;
          25.                 }
          26.                 if(!lowPriority.isEmpty()){
          27.                     System.out.print(lowPriority.remove());
          28.                     j++;
          29.                     k++;
          30.                     System.out.println(", j = "+j+", k="+k);
          31.                     break;
          32.                 }
          33.                 break;
          34.             }
          35.             try {
          36.                 Thread.sleep(100);
          37.             } catch (InterruptedException e) {
          38.                 e.printStackTrace();
          39.             }
          40.         }
          41.     }
          42. }

             
           
                  
                還有一種是,通過繼承PriorityQueue并實現Comparable接口,然后自已重寫過compareTo方法就能實現很強大的優先級隊列了,不過缺點是線程不安全的!
                代碼如下:
          1. package test;

          2. import java.util.PriorityQueue;

          3. public class PriorityTest extends PriorityQueue<PriorityTest.Test>{
          4.     static class Test implements Comparable<Test>{
          5.         String packet;
          6.         int priotity;
          7.         
          8.         public Test(String packet, int priotity) {
          9.             this.packet = packet;
          10.             this.priotity = priotity;
          11.         }
          12.         
          13.         public int compareTo(Test arg) { 
          14.             if(priotity < arg.priotity)
          15.                 return 1;
          16.             else if(priotity > arg.priotity)
          17.                 return -1;
          18.             else
          19.                 return 0;
          20.         } 
          21.         
          22.         public String toString(){
          23.             return packet; 
          24.         }
          25.     }
          26.     
          27.     public void add(String str, int priority){
          28.         super.add(new Test(str,priority));
          29.     }
          30.     
          31.     public static void main(String args[]){
          32.         PriorityTest pTest = new PriorityTest();
          33.         pTest.add("aaa",3);  //優先級最高
          34.         pTest.add("bbb",2);
          35.         pTest.add("ccc",1);
          36.         
          37.         while(!pTest.isEmpty()){
          38.             System.out.println(pTest.remove());
          39.         }
          40.     }
          41. }










          摘自:http://blog.csdn.net/liuzhengkang/archive/2009/01/05/3714047.aspx

          posted on 2009-08-15 17:25 石頭@ 閱讀(5525) 評論(1)  編輯  收藏 所屬分類: DS & Alg

          評論

          # re: java中關于優先級隊列的實現  回復  更多評論   

          如果要線程安全的優先級隊列不是有PriorityBlockingQueue么?不知道你用的哪個版本,不過jdk1.6里是有的
          2012-07-19 20:51 | 游刃

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 莒南县| 突泉县| 星子县| 温州市| 即墨市| 象州县| 天长市| 紫阳县| 阜新市| 石门县| 牟定县| 澳门| 三河市| 威远县| 正定县| 湾仔区| 宣化县| 安平县| 天气| 富源县| 云龙县| 浠水县| 华容县| 抚顺县| 阿拉善右旗| 灵山县| 嘉兴市| 阿勒泰市| 阳西县| 额尔古纳市| 巴林右旗| 临湘市| 兴城市| 汕头市| 威宁| 海阳市| 马关县| 辛集市| 伊金霍洛旗| 陆河县| 湖北省|