Master-Worker工具使用文件

weixin_34019929發表於2017-09-14

本案例測試Master-Worker併發設計模式,網上關於該模式,都大差不差,本案例使用執行緒池的方式,重新實現,並進行測試對比。筆者希望進行優化,並作為工具使用,技術水平有限,請多多指教,提出不同意見,共同進步。

建議Clone程式碼,學習
GIT倉庫地址

目錄

  • 使用方法
  • 測試對比
測試ThreadMap result:216474736
測試ExecutorService result:216474736
StopWatch '': running time (millis) = 652
-----------------------------------------
ms     %     Task name
-----------------------------------------
00432  066%  測試ThreadMap
00220  034%  測試ExecutorService

使用方法

SmileWorkerHandler

實現 SmileWorkerHandler 類,為每個任務寫處理方法

public interface SmileWorkerHandler<T> {
    default  T handler(T input) {
        return input;
    }
}

SmileMaster

該類任務主要用於任務分配,提供多種構造,執行自定義執行緒池和執行緒工廠

 /**
  * @param worker      Worker程式邏輯
  * @param countWorker Worker程式數量
  */
 public SmileMaster(SmileWorker worker, int countWorker)
 
 /**
  * @param worker        Worker程式邏輯
  * @param countWorker   Worker程式數量
  * @param threadFactory 自定義執行緒工廠
  */
 public SmileMaster(SmileWorker worker, int countWorker, ThreadFactory threadFactory)
 
 
  /**
   * @param worker   Worker程式邏輯
   * @param executor 執行緒池
   *                 使用執行緒池來維護工作佇列
   */
 public SmileMaster(SmileWorker worker, int countWorker, ExecutorService executor)

測試對比及使用程式碼

執行返回值就是本文開頭的程式碼塊,多餘的不解釋,直接擼程式碼,歡迎提出問題,共同解決,進步

/**
 * @Package: flyer.worker.core
 * @Description: 測試版本
 * @author: liuxin
 * @date: 2017/9/14 下午5:15
 */
public class SmileMasterTest {

    public static void main(String[] args) throws Exception {


        /**
         * 測試使用Map維護Thread
         */
        StopWatch watch = new StopWatch();
        watch.start("測試ThreadMap");
        Integer testthreadMapResult = testthreadMap(10,100000);
        System.out.println("測試ThreadMap result:"+testthreadMapResult);
        watch.stop();



        /**
         * 使用執行緒池維護現場
         */
        watch.start("測試ExecutorService");
        Integer testExecutorServiceResult = testExecutorService(10,100000);
        System.out.println("測試ExecutorService result:"+testExecutorServiceResult);
        watch.stop();
        System.out.println(watch.prettyPrint());
    }


    public static Integer testthreadMap(int countWorker,int countTask) {
        SmileMaster<Integer> smileMaster = new SmileMaster(new SmileWorker(new DefaultWorkerHandler() {
            @Override
            public Integer handler(Integer input) {
                return input * input;
            }
        }), countWorker);

        //提交100個子任務
        for (int i = 0; i < countTask; i++) {
            smileMaster.submit(i);
        }

        smileMaster.execute();

        Map<String, Object> resultMap = smileMaster.getResultMap();
        int re = 0;
        for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
            String key = entry.getKey();
            Integer o = (Integer) resultMap.get(key);
            re += o;
            resultMap.remove(key);
        }
        return re;
    }

    public static Integer testExecutorService(int countWorker,int countTask) {
        ExecutorService executorService2 = Executors.newCachedThreadPool();
        SmileMaster smileMaster1 = new SmileMaster(new SmileWorker(new DefaultWorkerHandler() {
            @Override
            public Integer handler(Integer input) {
                return input * input;
            }
        }), countWorker, executorService2);
        for (int i = 0; i < countTask; i++) {
            smileMaster1.submit(i);
        }
        smileMaster1.executePool();
        Map<String, Object> resultMapPool = smileMaster1.getResultMapPool();
        int re1 = 0;
        for (Map.Entry<String, Object> entry : resultMapPool.entrySet()) {
            String key = entry.getKey();
            Integer o = (Integer) resultMapPool.get(key);
            re1 += o;
            resultMapPool.remove(key);
        }
        return re1;
    }
}

相關文章