\(^_^)/ Java多线程Master-Worker模式

Master

package master_worker;

import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Master {
	// 任务队列
	protected Queue<Object> workQueue = new ConcurrentLinkedQueue<Object>();
	// Worker进程队列
	protected Map<String, Thread> threadMap = new HashMap<String, Thread>();
	// 子任务处理结果
	protected Map<String, Object> resultMap = new ConcurrentHashMap<String, Object>();

	// 是否子任务都结束了
	public boolean isCompelete() {
		for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
			if (entry.getValue().getState() != Thread.State.TERMINATED) {
				return false;
			}
		}
		return true;
	}

	// Master构造,需要一个Worker进程逻辑,和需要Worker进程数量
	public Master(Worker worker, int countWorker) {
		worker.setWorkQueue(workQueue);
		worker.setResultMap(resultMap);
		for (int i = 0; i < countWorker; i++) {
			threadMap.put(Integer.toString(i), new Thread(worker, Integer.toString(i)));
		}
	}

	// 提交一个任务
	public void submit(Object job) {
		workQueue.add(job);
	}

	// 开始运行所有的Worker进程,进行处理
	public void execute() {
		for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
			entry.getValue().start();
		}
	}

	// 返回子任务结果集
	public Map<String, Thread> getThreadMap() {
		return threadMap;
	}

}

 

 

Worker

package master_worker;

import java.util.Map;
import java.util.Queue;

public abstract class Worker implements Runnable {
	// 任务队列,用于取得子任务
	protected Queue<Object> workQueue;
	// 子任务的处理结果
	protected Map<String, Object> resultMap;

	@Override
	public void run() {
		while (true) {
			// 获取子任务
			Object input = workQueue.poll();
			if (input == null)
				break;
			// 处理子任务
			Object result = handle(input);
			// 将处理结果放入结果集
			resultMap.put(Integer.toString(input.hashCode()), result);
		}
	}

	// 子任务处理逻辑,在子类中实现具体逻辑
	public abstract Object handle(Object input);

	public void setResultMap(Map<String, Object> resultMap) {
		this.resultMap = resultMap;
	}

	public void setWorkQueue(Queue<Object> workQueue) {
		this.workQueue = workQueue;
	}

}

 

 

PlusWorker

package master_worker;

public class PlusWorker extends Worker {

	public Object handle(Object input){
		Integer i = (Integer)input;
		return i*i*i;
	}
}

 

 

Test

package master_worker;

import java.util.Map;
import java.util.Set;

public class Test {
	public static void main(String[] args) {
		Master master = new Master(new PlusWorker(), 10);
		for (int i = 0; i < 1000; i++) {
			master.submit(i);
		}
		master.execute();
		int result = 0;
		Map<String, Object> resultMap = master.getResultMap();
		while (resultMap.size() > 0 || !master.isCompelete()) {
			Set<String> keys = resultMap.keySet();
			String key = null;
			for (String k : keys) {
				key = k;
				break;
			}
			Integer i = null;
			if (key != null) {
				i = (Integer) resultMap.get(key);
			}
			if (i != null) {
				result += i;
			}
			if (key != null) {
				resultMap.remove(key);
			}
		}
		System.out.println(result);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值