Master-worker mode is one of the common parallel patterns, its core idea is that the system has two processes work together: master process, responsible for receiving and assigning tasks, worker process, responsible for processing subtasks. When the worker process finishes processing the child tasks, the results are returned to the master process, summarized by the master process, and finally the results are obtained.
First, what is the Master-worker mode:
Structure diagram for this pattern:
Structure diagram:
Worker: Used to actually handle a task;
Master: Assignment of tasks and synthesis of the final result;
Main: Start the program and the scheduler opens master.
Second, the code implementation:
The following is a simple implementation of the Master-worker framework.
(1) Master section:
[Java]View PlainCopy
- 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 Queue
- protected map<string,thread> threadmap= new hashmap<string,thread> ();
- //Sub-task processing result set
- protected map<string,object> resultmap= new concurrenthashmap<string, object> ();
- //Whether all sub-tasks 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, requires 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) {
- Workqueue.add (Job);
- }
- //Return child task result set
- Public map<string,object> Getresultmap () {
- return resultmap;
- }
- //Start running all worker processes for processing
- public Void execute () {
- for (map.entry<string, thread> entry:threadMap.entrySet ()) {
- Entry.getvalue (). Start ();
- }
- }
- }
(2) Worker process implementation:
[Java]View PlainCopy
- Package masterworker;
- Import Java.util.Map;
- Import Java.util.Queue;
- Public class Worker implements runnable{
- //Task queue for sub-task acquisition
- 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) {
- This.resultmap=resultmap;
- }
- //Sub-task processing logic, implementing specific logic in subclasses
- Public object handle (object input) {
- return input;
- }
- @Override
- public Void Run () {
- While (true) {
- //Get child tasks
- Object input= Workqueue.poll ();
- if (input==null) {
- Break ;
- }
- //Handling subtasks
- Object re = handle (input);
- Resultmap.put (Integer.tostring (Input.hashcode ()), re);
- }
- }
- }
(3) Using this small framework to calculate 1--100 's cubic and, Plusworker implementations:
[Java]View PlainCopy
- 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 PlainCopy
- Package masterworker;
- Import Java.util.Map;
- Import Java.util.Set;
- Public class Main {
- /**
- * @param args
- */
- public static void Main (string[] args) {
- //fixed use of 5 worker and specified worker
- Master m = new Master (new Plusworker (), 5);
- //Submit 100 sub-tasks
- For (int i=0;i<100;i++) {
- M.submit (i);
- }
- //Start calculation
- M.execute ();
- int re= 0;
- //Save Final settlement results
- Map<string,object> Resultmap =m.getresultmap ();
- //Do not wait for all workers to complete to begin calculating 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 result
- Re+=i;
- }
- if (key!=null) {
- //Remove items that have already been calculated
- Resultmap.remove (key);
- }
- }
- }
- }
Three, Summary:
Master-worker mode is a method of parallelization of serial tasks, the decomposed subtasks can be processed in parallel in the system, and if necessary, the master process does not have to wait for all subtasks to complete the calculation, it can calculate the final result set according to the existing partial result set.
Ext.: http://blog.csdn.net/lmdcszh/article/details/39698189
Multithreading: Multithreaded design Mode (iii): Master-worker mode