Worker是多线程核心类,所以做成抽象类,具体实现业务由子类来实现,此处为CalcWorker。
整个逻辑为计算从1加到100的实现
Task:
package com.mhm.masterworker;
/**
* 累加一共有多少钱
* @author Administrator
*
*/
public class Task {
private int id;
private String name;
private int 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 int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
Worker
package com.mhm.masterworker;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public abstract class Worker implements Runnable {
protected ConcurrentLinkedQueue<Task> workQueue;
protected 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) {
// poll会得到空
Task task = this.workQueue.poll();
if(task == null) {
break;
}
handle(task);
}
}
protected abstract void handle(Task task);
}
CalcWorker:
package com.mhm.masterworker;
public class CalcWorker extends Worker {
@Override
public void handle(Task task) {
Object output = null;
try {
// 耗时处理
Thread.sleep(500);
output = task.getPrice();
System.out.println(Thread.currentThread().getName() + ", output:" + output);
} catch (InterruptedException e) {
e.printStackTrace();
}
super.resultMap.put(String.valueOf(task.getId()), output);
}
}
Master
package com.mhm.masterworker;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Master {
// 装载任务的集合
private ConcurrentLinkedQueue<Task> workQueue = new ConcurrentLinkedQueue<>();
// 使用hashMap装载所有的worker对象
private HashMap<String, Thread> workerMap = new HashMap<>();
// 使用ConcurrentHashMap装载worker的并发执行的结果集
private ConcurrentHashMap<String, Object> resultMap = new ConcurrentHashMap<>();
public Master (Worker worker, int workerCount) {
for (int i = 0; i < workerCount; i++) {
// Worker worker = new Worker();
worker.setWorkQueue(workQueue);
worker.setResultMap(resultMap);
workerMap.put("节点"+i, new Thread(worker, "tid" + i));
}
}
public void addTask(Task task) {
this.workQueue.add(task);
}
// 启动应用程序,让所有的worker工作
public void execute() {
for (Map.Entry<String, Thread> me : workerMap.entrySet()) {
// 启动线程
// System.out.println(me.getKey() + "启动");
me.getValue().start();
}
}
public boolean isComplete() {
for (Map.Entry<String, Thread> me : workerMap.entrySet()) {
if(me.getValue().getState() != Thread.State.TERMINATED) {
return false;
}
}
return true;
}
// 把每个worker的结果累加起来
public int getResult() {
int r = 0;
for (Map.Entry<String, Object> me : resultMap.entrySet()) {
int v = (Integer)me.getValue();
r = r + v;
}
return r;
}
}
Main:
package com.mhm.masterworker;
public class Main {
public static void main(String[] args) {
Master master = new Master(new CalcWorker(), 10);
for(int i=0; i<100; i++) {
Task t = new Task();
t.setId(i);
t.setName("任务" + i);
t.setPrice(i+1);
master.addTask(t);
}
master.execute();
long start = System.currentTimeMillis();
while (true) {
if(master.isComplete()) {
int a = master.getResult();
System.out.println();
System.out.println("最终结果:" + String.valueOf(a) + ", 耗时:" + (System.currentTimeMillis() - start));
break;
}
}
}
}