The path of architecture--design pattern Master worker Parallel computing mode under multithreading

Source: Internet
Author: User

There are two important roles in this: Master, Worker

Master is responsible for receiving and assigning tasks, and the Worker handles subtasks assigned from master.


As shown in the figure: multiple clients send requests, the master processor receives requests and groups them and distributes them to multiple worker threads for execution.

Here's a small example to look at:

The directory structure is as follows:


Master class:


Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.concurrent.ConcurrentHashMap;
Import Java.util.concurrent.ConcurrentLinkedQueue;
/**
* Master Controller (receive and assign tasks)
* @author Administrator
*
*/
public class Master {

A collection of load-bearing tasks
Private concurrentlinkedqueue<task> Workqueue = new concurrentlinkedqueue<> ();
Load all Worker objects
Private hashmap<string, thread> workers = new hashmap<string,thread> ();
transferee the result set of concurrent processing tasks
Private concurrenthashmap<string, object> resultmap = new concurrenthashmap<string,object> ();

Public Master (worker worker, int workercount) {
Each worker object requires a concurrentlinkedqueue<task> reference
Worker.setworkerqueue (This.workqueue);
Every worker object needs concurrenthashmap<string, object> references
Worker.setresultmap (THIS.RESULTMAP);
for (int i = 0; i < Workercount; i++) {
Workers.put ("Sub-node" + integer.tostring (i), new Thread (worker));
System.out.println ("Sub-node" + i + "start");
}
}
Submit method
public void Submit (task Task) {
This.workQueue.add (Task);
}

Execute method, start worker, let all worker work
public void execute () {
For (map.entry<string, thread> me:workers.entrySet ()) {
Me.getvalue (). Start ();
}
}

To determine if a 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;
}
Return the result set
public int GetResult () {
int ret = 0;
For (map.entry<string, object> me:resultMap.entrySet ()) {
RET + + (Integer) me.getvalue ();
}
return ret;
}
}

Worker class


Import java.util.concurrent.ConcurrentHashMap;
Import java.util.concurrent.ConcurrentLinkedQueue;


public class Worker implements runnable{


Private concurrentlinkedqueue<task> workqueue;
Private concurrenthashmap<string, object> 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;
Output = Input.getprice ();
} catch (Exception e) {
E.printstacktrace ();
}
return output;
}


public void Setworkerqueue (concurrentlinkedqueue<task> workqueue) {
This.workqueue = Workqueue;
}

public void Setresultmap (concurrenthashmap<string, object> resultmap) {
This.resultmap = Resultmap;
}


}


Task Task Class

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


Test class


Import Java.util.Random;


public class Test {


public static void Main (string[] args) {

Master master = new Master (new Worker (), 10);

Random r = new Random ();
for (int i = 0; I <= i++) {
Task task = new Task ();
Task.setid (i);
Task.setname ("task" +i);
Task.setprice (R.nextint (1000));
SYSTEM.OUT.PRINTLN ("task queue" + task.getid () + "build");
Master.submit (Task);
}
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 consuming:" +end);
Break
}
}
}
}

Run Result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.