【Java互联网架构学习----001--17】并发模式Master-Worker模式

并发模式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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值