MasterWorker模式講解
阿新 • • 發佈:2018-12-27
import java.util.Random; public class Main { public static void main(String[] args) { Master master = new Master(new Worker(), 20); Random r = new Random(); for(int i = 1; i <= 100; i++){ Task t = new Task(); t.setId(i); t.setPrice(r.nextInt(1000)); master.submit(t); } master.execute(); long start = System.currentTimeMillis(); while(true){ if(master.isComplete()){ long end = System.currentTimeMillis() - start; int priceResult = master.getResult(); System.out.println("最終結果:" + priceResult + ", 執行時間:" + end); break; } } } }
import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; public class Master { //1 有一個盛放任務的容器 private ConcurrentLinkedQueue<Task> workQueue = new ConcurrentLinkedQueue<Task>(); //2 需要有一個盛放worker的集合 private HashMap<String, Thread> workers = new HashMap<String, Thread>(); //3 需要有一個盛放每一個worker執行任務的結果集合 private ConcurrentHashMap<String, Object> resultMap = new ConcurrentHashMap<String, Object>(); //4 構造方法 public Master(Worker worker , int workerCount){ // 每一個worker物件都需要有Master的引用 workerQueue用於任務的領取,resultMap用於任務的提交 worker.setWorkQueue(this.workQueue); worker.setResultMap(this.resultMap); for(int i = 0; i < workerCount; i ++){ // key表示每一個worker的名字,value表示執行緒執行物件 this.workers.put("子節點"+Integer.toString(i), new Thread(worker)); } } //5 需要一個提交任務的方法 public void submit(Task task){ this.workQueue.add(task); } //6 需要有一個執行的方法,啟動所有的worker方法去執行任務 public void execute(){ for(Map.Entry<String, Thread> me : workers.entrySet()){ me.getValue().start(); } } //7 判斷是否執行結束的方法 public boolean isComplete() { for(Map.Entry<String, Thread> me : workers.entrySet()){ if(me.getValue().getState() != Thread.State.TERMINATED){ return false; } } return true; } //8 計算結果方法 public int getResult() { int priceResult = 0; for(Map.Entry<String, Object> me : resultMap.entrySet()){ priceResult += (Integer)me.getValue(); } return priceResult; } }
public class Task {
private int id;
private int price ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; public class Worker implements Runnable { private ConcurrentLinkedQueue<Task> workQueue; private ConcurrentHashMap<String, Object> resultMap; public void setWorkQueue(ConcurrentLinkedQueue<Task> workQueue) { this.workQueue = workQueue; } public void setResultMap(ConcurrentHashMap<String, Object> resultMap) { this.resultMap = resultMap; } @Override public void run() { while(true){ Task input = this.workQueue.poll(); if(input == null) break; // 真正的去做業務處理 Object output = handle(input); this.resultMap.put(Integer.toString(input.getId()), output); } } private Object handle(Task input) { Object output = null; try { //處理任務的耗時。。 比如說進行操作資料庫。。。 Thread.sleep(500); output = input.getPrice(); } catch (InterruptedException e) { e.printStackTrace(); } return output; } }
public static void main(String[] args) {
System.out.println(Runtime.getRuntime().availableProcessors());
}