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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值