posts - 101,  comments - 29,  trackbacks - 0

          增加對結果的處理:

          1、修改Job,實現Callable接口

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

           

          2、修改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 static 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.         futres.add(pool.submit(job));  
          23.         //增加線程數  
          24.         synchronized (LOCK) {  
          25.             THREAD_COUNT++;  
          26.         }  
          27.     }  
          28.     /** 
          29.      * 統計任務結果 
          30.      */  
          31.     public List<Object> join(){  
          32.         synchronized (LOCK) {  
          33.             while(THREAD_COUNT > 0){//檢查線程數,如果為0,則表示所有任務處理完成  
          34.                 System.out.println("threadCount: "+THREAD_COUNT);  
          35.                 try {  
          36.                     LOCK.wait();//如果任務沒有全部完成,則掛起。等待完成的任務給予通知  
          37.                 } catch (InterruptedException e) {  
          38.                     e.printStackTrace();  
          39.                 }  
          40.             }  
          41.         }  
          42.         List<Object> list = new ArrayList<Object>();  
          43.         //取出每個任務的處理結果,匯總后返回  
          44.         for (Future<Object> future : futres) {  
          45.             try {  
          46.                 Object result = future.get();//因為任務都已經完成,這里直接get  
          47.                 list.add(result);  
          48.             } catch (Exception e) {  
          49.                 e.printStackTrace();  
          50.             }   
          51.         }  
          52.         return list;  
          53.     }  
          54. }  

           

           3、測試:

          Java代碼  收藏代碼
          1. public static void main(String[] args) {  
          2.         //初始化任務池  
          3.         Executer exe = new Executer(5);  
          4.         //初始化任務  
          5.         long time = System.currentTimeMillis();  
          6.         for (int i = 0; i < 10; i++) {  
          7.             MyJob job = new MyJob();  
          8.             exe.fork(job);//派發任務  
          9.         }  
          10.         //匯總任務結果  
          11.         List<Object> list = exe.join();  
          12.         System.out.println("Result: "+list);  
          13.         System.out.println("time: "+(System.currentTimeMillis() - time));  
          14.     }  

           

          4、執行結果:

          Java代碼  收藏代碼
          1. threadCount: 10  
          2. running thread id = 9  
          3. running thread id = 11  
          4. running thread id = 8  
          5. running thread id = 10  
          6. running thread id = 12  
          7. threadCount: 5  
          8. running thread id = 9  
          9. running thread id = 8  
          10. running thread id = 11  
          11. running thread id = 12  
          12. running thread id = 10  
          13. Result: [8910111281112910]  
          14. time: 2000  

           

          5、附件是完整代碼

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

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


          網站導航:
           
          主站蜘蛛池模板: 名山县| 隆尧县| 嘉祥县| 霍林郭勒市| 怀集县| 阳朔县| 嘉禾县| 金溪县| 精河县| 塔河县| 开封县| 宜黄县| 万宁市| 宁强县| 新竹县| 宁蒗| 武安市| 长沙市| 广宁县| 威远县| 万源市| 攀枝花市| 白朗县| 浏阳市| 乾安县| 杭州市| 漾濞| 溆浦县| 乐平市| 永仁县| 界首市| 佛坪县| 怀宁县| 盐津县| 兴隆县| 邓州市| 祁连县| 婺源县| 文山县| 松桃| 石首市|