posts - 101,  comments - 29,  trackbacks - 0

          優化鎖,之前的鎖是采用一個static的Object實現的,這要就會有一個問題,如果我創建了多個Executer,那么所有Job都會持有一把鎖,既影響性能,也容易出現死鎖的情況。所以,改成每個Executer持有一把鎖。

          Executer代碼如下:

           

          Java代碼  收藏代碼
          1. public class Executer {  
          2.     //計算已經派發的任務數(條件謂詞)  
          3.     public static int THREAD_COUNT = 0;  
          4.     //存儲任務的執行結果  
          5.     private List<Future<Object>> futres = new ArrayList<Future<Object>>();   
          6.     //條件隊列鎖  
          7.     public final Object lock = new Object();  
          8.     //線程池  
          9.     private ExecutorService pool = null;  
          10.     public Executer() {  
          11.         this(1);  
          12.     }  
          13.     public Executer(int threadPoolSize) {  
          14.         pool = Executors.newFixedThreadPool(threadPoolSize);  
          15.     }  
          16.     /** 
          17.      * 任務派發 
          18.      * @param job 
          19.      */  
          20.     public void fork(Job job){  
          21.         //設置同步鎖  
          22.         job.setLock(lock);  
          23.         //將任務派發給線程池去執行  
          24.         futres.add(pool.submit(job));  
          25.         //增加線程數  
          26.         synchronized (lock) {  
          27.             THREAD_COUNT++;  
          28.         }  
          29.     }  
          30.     /** 
          31.      * 統計任務結果 
          32.      */  
          33.     public List<Object> join(){  
          34.         synchronized (lock) {  
          35.             while(THREAD_COUNT > 0){//檢查線程數,如果為0,則表示所有任務處理完成  
          36.                 System.out.println("threadCount: "+THREAD_COUNT);  
          37.                 try {  
          38.                     lock.wait();//如果任務沒有全部完成,則掛起。等待完成的任務給予通知  
          39.                 } catch (InterruptedException e) {  
          40.                     e.printStackTrace();  
          41.                 }  
          42.             }  
          43.         }  
          44.         List<Object> list = new ArrayList<Object>();  
          45.         //取出每個任務的處理結果,匯總后返回  
          46.         for (Future<Object> future : futres) {  
          47.             try {  
          48.                 Object result = future.get();//因為任務都已經完成,這里直接get  
          49.                 list.add(result);  
          50.             } catch (Exception e) {  
          51.                 e.printStackTrace();  
          52.             }   
          53.         }  
          54.         return list;  
          55.     }  
          56. }  

           Job類:

           

          Java代碼  收藏代碼
          1. public abstract class Job implements Callable<Object> {  
          2.   
          3.     //鎖  
          4.     private Object lock = null;  
          5.   
          6.     void setLock(Object lock) {  
          7.         this.lock = lock;  
          8.     }  
          9.   
          10.     @Override  
          11.     public Object call() throws Exception {  
          12.         Object result = this.execute();//執行子類具體任務  
          13.         synchronized (lock) {  
          14.             //處理完業務后,任務結束,遞減線程數,同時喚醒主線程  
          15.             Executer.THREAD_COUNT--;  
          16.             lock.notifyAll();  
          17.         }  
          18.         return result;  
          19.     }  
          20.     /** 
          21.      * 業務處理函數 
          22.      */  
          23.     public abstract Object execute();  
          24.       
          25. }  

           測試結果:

           

          Java代碼  收藏代碼
          1. threadCount: 10  
          2. running thread id = 8  
          3. running thread id = 10  
          4. running thread id = 9  
          5. running thread id = 12  
          6. running thread id = 11  
          7. threadCount: 8  
          8. threadCount: 7  
          9. threadCount: 6  
          10. threadCount: 5  
          11. running thread id = 12  
          12. running thread id = 8  
          13. running thread id = 11  
          14. threadCount: 2  
          15. running thread id = 10  
          16. threadCount: 1  
          17. running thread id = 9  
          18. ResultSize: 10  
          19. time: 2001  

           OK!

          這樣每個Executer就可以使用自己的lock,而相互不受同步的影響

          posted on 2012-07-15 01:21 mixer-a 閱讀(1112) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 长乐市| 永定县| 玉树县| 通州区| 丰台区| 桂平市| 济南市| 双鸭山市| 彩票| 鄂托克旗| 汤阴县| 仁寿县| 晴隆县| 裕民县| 高碑店市| 嫩江县| 桂东县| 罗江县| 理塘县| 宣汉县| 汕头市| 修武县| 靖州| 于田县| 金阳县| 隆回县| 河曲县| 玉门市| 齐河县| 正蓝旗| 宜州市| 沿河| 安远县| 宜兰县| 金坛市| 从化市| 富民县| 灵台县| 保定市| 河曲县| 滁州市|