Master-worker mode is one of the common parallel patterns, its core idea is that the system has two processes working together: master process, responsible for receiving and assigning tasks, and worker processes, responsible for handling subtasks. When the worker process completes the subtasks, the results are returned to the master process, summed up by the master process, and the final results are obtained.
First, what is the Master-worker mode:
Structure diagram for this pattern:
Structure Chart:
Worker: Used to actually handle a task;
Master: The distribution of tasks and the synthesis of final results;
Main: Start the program, scheduling to open master.
Second, code implementation:
The following is a simple Master-worker framework implementation.
(1) Master part: [java] View plain copy package masterworker; 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 { //task Queue protected Queue< Object> workqueue= new concurrentlinkedqueue<object> (); //worker process Queues protected Map<String ,Thread> Threadmap= new hashmap<string ,thread> (); //subtasks processing result sets protected map<string ,object> resultmap= new concurrenthashmap<string, object> (); //whether all subtasks are over public Boolean iscomplete () { for (Map.Entry<String , thread> entry:threadmap.entryset ()) { if (Entry.getvalue (). GetState ()!=thread.state.terminated) { return false; } } return true ; } //maSter constructs require a worker process logic, and the number of worker processes required 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)); } } //Submit a Task public void submit (object job) { &Nbsp; workqueue.add (Job); } // Returns the subtask result set public map<string ,object> getresultmap () { return resultMap; } //starts running all the worker processes, processing public void execute () { for (map.entry<string , thread > entry:threadmap.entryset ()) { entry.getvalue (). Start (); } } }
(2) Worker process implementation:
[java] View plain copy package masterworker; import java.util.map; import java.util.queue; public class worker implements runnable{ //task queues, for subtasks protected Queue<Object> workQueue; // Sub-task processing result set protected Map<String ,Object> resultMap; public void setworkqueue (queue<object> workqueue) { this.workQueue= workQueue; } public void setresultmap (map<string ,object> resultmap) { thThe logic of is.resultmap=resultmap; } //subtask processing, Implement specific Logic public object handle (object input) in subclasses { return input; } @ override public void run () { while (True) { //get subtasks object input= workqueue.poll (); if (input==null) { break; } //Process Subtasks object re = handle (input); resultmap.put (Integer.tostring (Input.hashcode ()), re); } } }
(3) Using this small framework to compute the cubic and plusworker of 1--100:
[Java] View plain copy package masterworker; public class Plusworker extends Worker {@Override public object handle (object input) { Integer i = (integer) input; return i*i*i; } }
(4) The main function for the calculation:
[java] View plain copy package masterworker; import java.util.map; import java.util.set; public class main { /** * @param args */ public static void main (String[] args) { &NBSP;&NBSP;//uses 5 worker and specifies worker master m = new master (New plusworker (), 5); //Submit 100 Subtasks for (int i=0;i<100;i + +) { m.submit (i); &NBsp; } // Start calculation m.execute (); int re= 0; Save Final settlement Results Map<String ,Object> Resultmap =m.getresultmap (); //does not need to wait for all of the worker's execution to complete, to start computing the final result while (resultmap.size () >0 | | !m.iscomplete ()) { 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) { //Final Results re+=i; } if (key!=null) { //removes items that have been calculated resultmap.remove (key); } } } } &nBsp
Third, Summary:
Master-worker mode is a scheme of parallelization of serial tasks, where the decomposed subtasks can be processed in parallel in the system, and if necessary, the master process does not need to wait for all subtasks to complete the calculation, and can compute the final result set based on the existing partial result set.