Master-Worker模式
Master-Worker模式是常用的并行计算模式。它的核心实现是系统由两类进程协作工作:Master进程和Worker进程。Master负责接收和分配任务,Worker负责处理子任务。当各个Worker子进程处理完成后,会将结果返回给Master,由Master做归纳和总结。其好处是能将一个大任务分解成若干个小任务,并行执行,从而提高系统的吞吐量。
代码示例
任务类
/**
* 任务
* @author shixiangcheng
* 2019-08-03
*/
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;
}
}
Master 类
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Master
* @author shixiangcheng
* 2019-08-03
*/
public class Master {
//1.一个盛放任务的容器
private ConcurrentLinkedQueue<Task> workQueue=new ConcurrentLinkedQueue<Task>();
//2.一个盛放worker的集合
private Map<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.setWorkQueue(this.workQueue);
worker.setResultMap(this.resultMap);
for(int i=0;i<workerCount;i++){
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;
}
}
Worker类
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Worker
* @author shixiangcheng
* 2019-08-03
*/
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;
}
}
Main类
import java.util.Random;
/**
* 主类
* @author shixiangcheng
* 2019-08-03
*/
public class Main {
public static void main(String [] args){
Master master=new Master(new Worker(),10);
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;
}
}
}
}
运行主类的结果:一共100个任务,分给10个worker,每个worker处理10个任务。每个任务历时0.5s,一共是5s左右。
最终结果:52065,执行时间5002