Master/Worker mode

package test;

import java.util.Random;

public class Main {

	public static void main(String[] args) {
		System.out.println("Number of processors available on my machine:" + Runtime.getRuntime().availableProcessors());
		Master master = new Master(new MyWorker(), Runtime.getRuntime().availableProcessors());
		Random r = new Random();
		for(int i = 1; i<= 100; i++){
			Task t = new Task();
			t.setId(i);
			t.setName("任务"+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 ret = master.getResult();
				System.out.println("Final result: " + ret + ", execution time: " + end);
				break;
			}
		}
		
	}
}

 

package test;

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

public class Master {

	//1 should have a collection of hosting tasks
	private ConcurrentLinkedQueue<Task> workQueue = new ConcurrentLinkedQueue<Task>();
	
	//2 Use HashMap to host all worker objects
	private HashMap<String, Thread> workers = new HashMap<String, Thread>();
	
	//3 Use a container to hold the result set of each worker not executing the task
	private ConcurrentHashMap<String, Object> resultMap = new ConcurrentHashMap<String, Object>();
	
	//4 Constructor
	public Master(Worker worker, int workerCount){
		// Each worker object needs to have a reference to the Master workQueue for task collection, and resultMap for task submission
		worker.setWorkerQueue(this.workQueue);
		worker.setResultMap(this.resultMap);
		for(int i = 0 ; i < workerCount; i++){
			//key represents the name of each worker, value represents the thread execution object
			workers.put("子节点" + Integer.toString(i), new Thread(worker));
		}
	}
	
	//5 submit method
	public void submit(Task task){
		this.workQueue.add(task);
	}
	
	//6 There needs to be a method to execute (start the application to make all workers work)
	public void execute(){
		for(Map.Entry<String, Thread> me : workers.entrySet()){
			me.getValue().start();
		}
	}

	//8 Determine whether the thread has finished executing
	public boolean isComplete() {
		for(Map.Entry<String, Thread> me : workers.entrySet()){
			if(me.getValue().getState() != Thread.State.TERMINATED){
				return false;
			}
		}		
		return true;
	}

	//9 return result set data
	public int getResult() {
		int ret = 0;
		for(Map.Entry<String, Object> me : resultMap.entrySet()){
			// Aggregate logic..
			ret += Integer.parseInt((String)me.getValue());
		}
		return ret;
	}
}

 

package test;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Worker implements Runnable {

	private ConcurrentLinkedQueue<Task> workQueue;
	private ConcurrentHashMap<String, Object> resultMap;
	
	public void setWorkerQueue(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;
			//Real do business processing
			Object output = MyWorker.handle(input);
			this.resultMap.put(Integer.toString(input.getId()), output);
		}
	}
	
	public static Object handle(Task input) {
		return null;
	}


}

 

package test;

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

 

package test;

public class MyWorker extends Worker {
	
	public static Object handle(Task input) {
		Object output = null;
		try {
			//Indicates the time-consuming processing of task tasks, which may be data processing or database manipulation...
			Thread.sleep(500);
			output = input.getPrice();
		} catch (InterruptedException e) {
			e.printStackTrace ();
		}
		return output;
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123817&siteId=291194637