用PriorityBlockingQueue简化线程优先级调度策略

来源:互联网 发布:java如何转换日期格式 编辑:程序博客网 时间:2024/04/30 08:31
我们只需要把放入该队列的对象实现Comparable接口就可以轻松实现线程优先级调度了。 
下面写一个简单的demo帮助理解吧。 
背景:一个车站,有固定车位,有一个出站口。我们知道车站有快车慢车,长途短途等之分。在此我们统一简化为出站优先级,出站顺序根据站内车优先级别排列。 

Java代码  收藏代码
  1. import java.util.concurrent.PriorityBlockingQueue;  
  2.   
  3. public class ThreadPriorityScheduleDemo {  
  4.   
  5.        
  6.     private static final int MAX_PARKING = 50;  
  7.   
  8.     /** 
  9.      * @param args 
  10.      */  
  11.     public static void main(String[] args) {  
  12.           
  13.         ThreadPriorityScheduleDemo instance = new ThreadPriorityScheduleDemo();  
  14.           
  15.         instance.busEnter(new Bus("粤A12345"25));  
  16.         instance.busEnter(new Bus("粤A88888"12));  
  17.         instance.busEnter(new Bus("粤A66666"26));  
  18.         instance.busEnter(new Bus("粤A33333"17));  
  19.         instance.busEnter(new Bus("粤A21123"21));  
  20.         instance.busEnter(new Bus("粤AGG892"14));  
  21.         instance.busEnter(new Bus("粤AJJ000"29));  
  22.           
  23.         while(true){  
  24.             instance.busQuit();  
  25.         }  
  26.     }  
  27.       
  28.     private static final PriorityBlockingQueue<Bus> busStation = new PriorityBlockingQueue<Bus>(MAX_PARKING);  
  29.       
  30.     /** 
  31.      * 车辆进站 
  32.      * @param bus 
  33.      */  
  34.     private void busEnter(Bus bus){  
  35. //      System.out.println("进站-->"+bus.toString());  
  36.         if(busStation.size()<MAX_PARKING)  
  37.             busStation.add(bus);  
  38.         else  
  39.             System.out.println("站内车位已满");  
  40.           
  41.     }  
  42.       
  43.     /** 
  44.      * 车辆出站 
  45.      */  
  46.     private void busQuit(){  
  47.         try {  
  48.             Bus bus = busStation.take();  
  49.             System.out.println("出站-->"+bus.toString());  
  50.         } catch (InterruptedException e) {  
  51.               
  52.         }  
  53.     }  
  54.       
  55.     /** 
  56.      * 车实例 
  57.      * @author jiangw 
  58.      * 
  59.      * 2010-3-26 
  60.      */  
  61.     static class Bus implements Comparable<Bus>{  
  62.           
  63.         private String busNo;  
  64.         private Integer busType;  
  65.         private Integer level;  
  66.         private Bus() {}  
  67.   
  68.         private Bus(String busNo, Integer busType, Integer level) {  
  69.             super();  
  70.             this.busNo = busNo;  
  71.             this.busType = busType;  
  72.             this.level = level;  
  73.         }  
  74.   
  75.         public String getBusNo() {  
  76.             return busNo;  
  77.         }  
  78.   
  79.         public void setBusNo(String busNo) {  
  80.             this.busNo = busNo;  
  81.         }  
  82.   
  83.         public Integer getBusType() {  
  84.             return busType;  
  85.         }  
  86.   
  87.         public void setBusType(Integer busType) {  
  88.             this.busType = busType;  
  89.         }  
  90.           
  91.   
  92.         public Integer getLevel() {  
  93.             return level;  
  94.         }  
  95.   
  96.         public void setLevel(Integer level) {  
  97.             this.level = level;  
  98.         }  
  99.   
  100.         @Override  
  101.         public int compareTo(Bus o) {  
  102.             if(o instanceof Bus){  
  103.                 return (level>o.level)?1:-1;  
  104.             }  
  105.             return 0;  
  106.         }  
  107.   
  108.         @Override  
  109.         public String toString() {  
  110.             return "当前车信息:种类["+busType+"]车牌["+busNo+"]优先级["+level+"]";  
  111.         }  
  112.     }  
  113.   
  114. }  


运行结果: 
出站-->当前车信息:种类[2]车牌[粤A21123]优先级[1] 
出站-->当前车信息:种类[1]车牌[粤A88888]优先级[2] 
出站-->当前车信息:种类[1]车牌[粤AGG892]优先级[4] 
出站-->当前车信息:种类[2]车牌[粤A12345]优先级[5] 
出站-->当前车信息:种类[2]车牌[粤A66666]优先级[6] 
出站-->当前车信息:种类[1]车牌[粤A33333]优先级[7] 
出站-->当前车信息:种类[2]车牌[粤AJJ000]优先级[9] 
0 0