并发模式Master-Worker模式核心思想:系统由两类进程协同工作,即Master进程和Worker进程,Master负责接收和分配任务,Wroker负责处理子任务。当各个Worker进程将子任务处理完成后,将结果返回给Master进程,由Master进程进行汇总,从而得到最终的结果.Master接收到了一个任务,对任务进行拆分,并且分配给各个Worker,让各个Worker之间各自运行各自的子任务,最后Worker们的返回结果交给Master进行汇总并且最后返回给任务的发起方
主要参与者
Worker:用于实际处理一个任务;
Master:任务的分配和最终结果的合成;
Main:启动程序,调度开启Master。
package com.neo.study001.redio17;
import java.util.Random;
/**
* @author liyy
* @date 2020/5/6 22:41
*/
public class TestMain {
public static void main(String[] args) {
Master master = new Master(new Worker(), 10);
Random random = new Random();
for (int i = 0; i <= 100; i++) {
Task task = new Task();
task.setId(i);
task.setName("任务" + i);
task.setPrice(random.nextDouble());
master.submit(task);
}
master.excute();
long start = System.currentTimeMillis();
while (true) {
if (master.isComplete()) {
long time= System.currentTimeMillis()-start;
double result = master.getResult();
System.out.println("最终结果:" + result+",最终耗时:"+time);
break;
}
}
}
}
package com.neo.study001.redio17;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* @author liyy
* @date 2020/5/6 22:15
*/
public class Master {
//1.承装任务的集合
private ConcurrentLinkedQueue<Task> workQueue = new ConcurrentLinkedQueue<Task>();
//2.承装所有的Worker对象
private HashMap<String, Thread> workers = new HashMap<String, Thread>();
//3.承装结果集
private ConcurrentHashMap<String, Object> resultMap = new ConcurrentHashMap<String, Object>();
public Master(Worker worker, int workerCount) {
worker.setWorkQueue(this.workQueue);
worker.setResultMap(this.resultMap);
for (int i = 0; i < workerCount; i++) {
workers.put("子节点" + i, new Thread(worker));
}
}
//提交方法
public void submit(Task task) {
this.workQueue.add(task);
}
public void excute() {
for (Map.Entry<String, Thread> me : workers.entrySet()) {
me.getValue().start();
}
}
public boolean isComplete() {
for (Map.Entry<String, Thread> me : workers.entrySet()) {
if (me.getValue().getState() != Thread.State.TERMINATED) {
return false;
}
}
return true;
}
public double getResult() {
double sum=0D;
for (Map.Entry<String, Object> e :
resultMap.entrySet()) {
sum+=(double)e.getValue();
}
return sum;
}
}
package com.neo.study001.redio17;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* @author liyy
* @date 2020/5/6 22:26
*/
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 obj=handle(input);
this.resultMap.put(Integer.toString(input.getId()),obj);
}
}
private Object handle(Task input) {
Object result=null;
try {
Thread.sleep(500);
result = input.getPrice();
} catch (InterruptedException e) {
e.printStackTrace();
}
return result;
}
}
package com.neo.study001.redio17;
/**
* @author liyy
* @date 2020/5/6 22:17
*/
public class Task {
private int id;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}