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);
}
}