posts - 101,  comments - 29,  trackbacks - 0

          優(yōu)化鎖,之前的鎖是采用一個(gè)static的Object實(shí)現(xiàn)的,這要就會(huì)有一個(gè)問(wèn)題,如果我創(chuàng)建了多個(gè)Executer,那么所有Job都會(huì)持有一把鎖,既影響性能,也容易出現(xiàn)死鎖的情況。所以,改成每個(gè)Executer持有一把鎖。

          Executer代碼如下:

           

          Java代碼  收藏代碼
          1. public class Executer {  
          2.     //計(jì)算已經(jīng)派發(fā)的任務(wù)數(shù)(條件謂詞)  
          3.     public static int THREAD_COUNT = 0;  
          4.     //存儲(chǔ)任務(wù)的執(zhí)行結(jié)果  
          5.     private List<Future<Object>> futres = new ArrayList<Future<Object>>();   
          6.     //條件隊(duì)列鎖  
          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.      * 任務(wù)派發(fā) 
          18.      * @param job 
          19.      */  
          20.     public void fork(Job job){  
          21.         //設(shè)置同步鎖  
          22.         job.setLock(lock);  
          23.         //將任務(wù)派發(fā)給線程池去執(zhí)行  
          24.         futres.add(pool.submit(job));  
          25.         //增加線程數(shù)  
          26.         synchronized (lock) {  
          27.             THREAD_COUNT++;  
          28.         }  
          29.     }  
          30.     /** 
          31.      * 統(tǒng)計(jì)任務(wù)結(jié)果 
          32.      */  
          33.     public List<Object> join(){  
          34.         synchronized (lock) {  
          35.             while(THREAD_COUNT > 0){//檢查線程數(shù),如果為0,則表示所有任務(wù)處理完成  
          36.                 System.out.println("threadCount: "+THREAD_COUNT);  
          37.                 try {  
          38.                     lock.wait();//如果任務(wù)沒(méi)有全部完成,則掛起。等待完成的任務(wù)給予通知  
          39.                 } catch (InterruptedException e) {  
          40.                     e.printStackTrace();  
          41.                 }  
          42.             }  
          43.         }  
          44.         List<Object> list = new ArrayList<Object>();  
          45.         //取出每個(gè)任務(wù)的處理結(jié)果,匯總后返回  
          46.         for (Future<Object> future : futres) {  
          47.             try {  
          48.                 Object result = future.get();//因?yàn)槿蝿?wù)都已經(jīng)完成,這里直接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();//執(zhí)行子類具體任務(wù)  
          13.         synchronized (lock) {  
          14.             //處理完業(yè)務(wù)后,任務(wù)結(jié)束,遞減線程數(shù),同時(shí)喚醒主線程  
          15.             Executer.THREAD_COUNT--;  
          16.             lock.notifyAll();  
          17.         }  
          18.         return result;  
          19.     }  
          20.     /** 
          21.      * 業(yè)務(wù)處理函數(shù) 
          22.      */  
          23.     public abstract Object execute();  
          24.       
          25. }  

           測(cè)試結(jié)果:

           

          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!

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

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 雷州市| 清水河县| 定西市| 长治市| 汨罗市| 伊宁市| 高阳县| 珠海市| 色达县| 临安市| 分宜县| 泰宁县| 盘山县| 井陉县| 丰宁| 闻喜县| 登封市| 偃师市| 双辽市| 阿克| 德令哈市| 简阳市| 兰西县| 怀安县| 南华县| 武义县| 凤翔县| 宜丰县| 崇信县| 济南市| 乌鲁木齐县| 汕头市| 米泉市| 彰武县| 罗江县| 桃园市| 榆树市| 肇源县| 云霄县| 巨野县| 浦城县|