直接上代码:
Master的类
public class Master
{
// 1 有一个盛放任务的容器
private ConcurrentLinkedQueue workQueue = new ConcurrentLinkedQueue();
// 2 需要有一个盛放worker的集合
private HashMap workers = new HashMap();
// 3 需要有一个盛放每一个worker执行任务的结果集合
private ConcurrentHashMap resultMap = new ConcurrentHashMap();
// 4 构造方法
public Master(Worker worker, int workerCount)
{
worker.setWorkQueue(this.workQueue);
worker.setResultMap(this.resultMap);
for (int i = 0; i < workerCount; i++)
{
this.workers.put(Integer.toString(i), new Thread(worker));
}
}
// 5 需要一个提交任务的方法
public void submit(Task task)
{
this.workQueue.add(task);
}
// 6 需要有一个执行的方法,启动所有的worker方法去执行任务
public void execute()
{
for (Map.Entry me : workers.entrySet())
{
me.getValue().start();
}
}
// 7 判断是否运行结束的方法
public boolean isComplete()
{
for (Map.Entry me : workers.entrySet())
{
if (me.getValue().getState() != Thread.State.TERMINATED)
{
return false;
}
}
return true;
}
// 8 计算结果方法
public int getResult()
{
int priceResult = 0;
for (Map.Entry me : resultMap.entrySet())
{
priceResult += (Integer) me.getValue();
}
return priceResult;
}
}
Worker的类
public class Worker implements Runnable
{
private ConcurrentLinkedQueue workQueue;
private ConcurrentHashMap resultMap;
public void setWorkQueue(ConcurrentLinkedQueue workQueue)
{
this.workQueue = workQueue;
}
public void setResultMap(ConcurrentHashMap resultMap)
{
this.resultMap = resultMap;
}
@Override
public void run()
{
while (true)
{
Task input = this.workQueue.poll();
if (input == null)
break;
Object output = handle(input);
this.resultMap.put(Integer.toString(input.getId()), output);
}
}
private Object handle(Task input)
{
Object output = null;
try
{
// 处理任务的耗时。。 比如说进行操作数据库。。。
Thread.sleep(500);
output = input.getPrice();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return output;
}
}
Task类:
public class Task
{
private int id;
private int price;
Main类:
public static void main(String[] args) {
Master master = new Master(new Worker(), 20);
Random r = new Random();
for(int i = 1; i <= 100; i++){
Task t = new Task();
t.setId(i);
t.setPrice(r.nextInt(1000));
master.submit(t);
}
master.execute();
long start = System.currentTimeMillis();
while(true){
if(master.isComplete()){
long end = System.currentTimeMillis() - start;
int priceResult = master.getResult();
System.out.println("最终结果:" + priceResult + ", 执行时间:" + end);
break;
}
}