關於優先順序隊列的實現

來源:互聯網
上載者:User
   

        這幾天一直在搞關於優先順序隊列的實現,因為要考慮到線程的安全,所以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. }

     

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.