国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
基于優(yōu)先級隊列java線程池

背景

最近在看同事的消息平臺的問題進行優(yōu)化,其中一點就是推送來的消息沒有區(qū)分優(yōu)先級,造成實時性要求高的不能優(yōu)先滿足,被全網下發(fā)的普通優(yōu)先級占用了,造成消息延遲。

對應的改進一點就是采用把現有線程池改為優(yōu)先級隊列。

實現

創(chuàng)建一個RunnablePriority,它實現Runnable接口和參數化為RunnablePriority類的Comparable接口。

  1. package thread;  
  2.   
  3.   
  4.    
  5. /** 
  6.  * 優(yōu)先級比較 
  7.  *  
  8.  */  
  9.    
  10. public class RunnablePriority  implements Runnable, Comparable<RunnablePriority> {  
  11.    
  12.     private int priority;     
  13.       
  14.     public int getPriority() {  
  15.         return priority;  
  16.     }  
  17.   
  18.   
  19.     public RunnablePriority(int priority) {  
  20.         this.priority = priority;  
  21.     }  
  22.       
  23.    
  24.     @Override  
  25.     public int compareTo(RunnablePriority o) {  
  26.         // 復寫此方法進行任務執(zhí)行優(yōu)先級排序  
  27.         // return priority < o.priority ? -1 : (priority > o.priority ? 1 : 0);  
  28.         // System.out.println(priority +"::"+ o.priority);  
  29.         if (this.getPriority() < o.priority) {  
  30.             return 1;  
  31.         }   
  32.         if(this.getPriority()>o.priority){  
  33.             return -1;  
  34.         }  
  35.         return 0;  
  36.     }  
  37.    
  38.     @Override  
  39.     public void run() {  
  40.         System.out.printf("RunnablePriority: %s Priority :%d\n",Thread.currentThread().getName(),priority);  
  41.         try {  
  42.             Thread.sleep(100);  
  43.         } catch (InterruptedException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.         // 執(zhí)行任務代碼..  
  47.     }  
  48.    
  49. }  
這個類實現聲明在Comparable接口中的compareTo()方法。它接收一個RunnablePriority對象作為參數,比較這兩個對象(當前對象和參數對象)的優(yōu)先級。讓優(yōu)先級高的任務先于優(yōu)先級低的任務執(zhí)行。

其中run方法用來處理業(yè)務。

測試主類如下:

  1. package thread;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.concurrent.ExecutorService;  
  4. import java.util.concurrent.PriorityBlockingQueue;  
  5. import java.util.concurrent.ThreadPoolExecutor;  
  6. import java.util.concurrent.TimeUnit;  
  7.   
  8.            
  9.     /** 
  10.      * 線程池隊列插隊Demo,自定義線程池然后使用PriorityBlockingQueue類實現, 
  11.      * 
  12.      */  
  13.        
  14.     public class ThreadExecutor {  
  15.         public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss::SSS");  
  16.         // public ExecutorService singleThreadExecutor =  
  17.         // Executors.newSingleThreadExecutor();  
  18.                
  19.         static int count =0;  
  20.         public static void main(String[] args) {  
  21.               
  22.             ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 2, 1, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>());  
  23.             //模擬加入消息  
  24.             for (int i = 0; i < 5; i++) {  
  25.                 System.out.println(ThreadExecutor.sdf.format(System.currentTimeMillis()) + "加入消息~~~加入隊列第" + (++count) + "條消息!");  
  26.                 executor.execute(new RunnablePriority(1));                
  27.             }  
  28.             
  29.             
  30.             try {  
  31.                 Thread.sleep(100);  
  32.             } catch (InterruptedException e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.             //模擬插入消息  
  36.             for (int i = 0; i < 5; i++) {  
  37.                      
  38.                 System.out.println(ThreadExecutor.sdf.format(System.currentTimeMillis()) + "插入消息~~~~插入隊列第" + (++count) + "條消息!");  
  39.                 executor.execute(new RunnablePriority(5));  
  40.             }  
  41.             try {  
  42.                 Thread.sleep(10000);  
  43.             } catch (InterruptedException e) {  
  44.                 e.printStackTrace();  
  45.             }  
  46.             //結束  
  47.             executor.shutdown();  
  48.             try {                 
  49.                 executor.awaitTermination(1, TimeUnit.DAYS);  
  50.                   
  51.                 } catch (InterruptedException e) {                
  52.                 e.printStackTrace();                  
  53.             }  
  54.             System.out.println("over");  
  55.         }  
  56.        
  57.     }  

這個類創(chuàng)建一個ThreadPoolExecutor對象,名為executor。使用參數化為Runnable接口的PriorityBlockingQueue作為執(zhí)行者用來存儲待處理任務的隊列。

然后分別模擬插入不同優(yōu)先級任務,最后結束。

運行結果如下:

  1. 2016-04-26 13:58:55::447加入消息~~~加入隊列第1條消息!  
  2. 2016-04-26 13:58:55::449加入消息~~~加入隊列第2條消息!  
  3. 2016-04-26 13:58:55::449加入消息~~~加入隊列第3條消息!  
  4. 2016-04-26 13:58:55::449加入消息~~~加入隊列第4條消息!  
  5. 2016-04-26 13:58:55::449加入消息~~~加入隊列第5條消息!  
  6. RunnablePriority: pool-1-thread-1 Priority :1  
  7. RunnablePriority: pool-1-thread-2 Priority :1  
  8. 2016-04-26 13:58:55::549插入消息~~~~插入隊列第6條消息!  
  9. 2016-04-26 13:58:55::549插入消息~~~~插入隊列第7條消息!  
  10. 2016-04-26 13:58:55::549插入消息~~~~插入隊列第8條消息!  
  11. 2016-04-26 13:58:55::549插入消息~~~~插入隊列第9條消息!  
  12. 2016-04-26 13:58:55::549插入消息~~~~插入隊列第10條消息!  
  13. RunnablePriority: pool-1-thread-2 Priority :5  
  14. RunnablePriority: pool-1-thread-1 Priority :5  
  15. RunnablePriority: pool-1-thread-1 Priority :5  
  16. RunnablePriority: pool-1-thread-2 Priority :5  
  17. RunnablePriority: pool-1-thread-1 Priority :5  
  18. RunnablePriority: pool-1-thread-2 Priority :1  
  19. RunnablePriority: pool-1-thread-2 Priority :1  
  20. RunnablePriority: pool-1-thread-1 Priority :1  
  21. over  

運行過程:

我們將執(zhí)行者轉換成一個基于優(yōu)先級的(執(zhí)行者)。你只要傳入一個參數化為Runnable接口的PriorityBlockingQueue對象作為參數。但是,使用執(zhí)行者時,你應該知道存儲在優(yōu)先級列隊中的所有對象必須實現Comparable接口。

我們已經實現了RunnablePriority類,它實現了Runnable接口和Comparable接口,它被存儲在優(yōu)先級隊列中。這個類有一個Priority屬性,用來存儲任務的優(yōu)先級。如果一個任務的這個屬性有更高的值,它將被更早的執(zhí)行。compareTo()方法決定任務在優(yōu)先級列隊中的順序。在Main類,你提交10個不同優(yōu)先級的任務給執(zhí)行者。你提交給執(zhí)行者的第一個任務將第一個被執(zhí)行。由于執(zhí)行者閑置的,正在等待任務被執(zhí)行,當第一個任務到達執(zhí)行者時,執(zhí)行者立即執(zhí)行它們。你已經創(chuàng)建有2個執(zhí)行線程的執(zhí)行者,所以,前兩個任務將第一個被執(zhí)行。然后,剩下的任務將按它們的優(yōu)先級來執(zhí)行。就是有限執(zhí)行5,再執(zhí)行剩下的1.

本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java 線程優(yōu)先級與yield()
Python數據結構與算法(9)——優(yōu)先級隊列queue例程
Java線程優(yōu)先級設置
Java之線程初步
淺議Visual C++多線程設計
線程學習
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服