Master-Worker mode,

Source: Internet
Author: User

Master-Worker mode,

Master-Worker Mode

The system of a certain part of functions is divided into two roles: Master and Worker to process requests.

The Master is responsible for the requests sent by the client, the feedback of the return processing results, and the assignment of tasks. Worker is used to process subtasks and report the results to the Master. This mode is a bit similar to the ancient sub-seal system.

  

For example, the Master splits the task and puts it into the task queue. Then, the Worker is triggered to process the task. The Master only performs task receiving, splitting, and result statistics, and specifies the number of workers and performance indicators. However, it does not participate in actual Worker management, but is handed over to third-party scheduling monitoring and scheduling Worker. In this way, we can divide a relatively large task into small tasks for processing, greatly improving the system performance. In the above image, we do not have to use the above container, you can choose as needed.

Public class MasterMain {public static void main (String [] args) {Master master = new Master (new Worker (), 10); for (int I = 10; I <= 100; I ++) {Task task = new Task (); task. setId (I); task. setName ("task" + I); task. setPrice (I); master. submit (task);} master.exe cute (); long start = System. currentTimeMillis (); while (true) {if (master. isExist () {long end = System. currentTimeMillis ()-start; long result = master. getResult (); System. out. println ("task execution time:" + end + "=== result set:" + result); break ;}}}}
Public class Master {// 1. concurrentincluqueue <Object> workqueue = new concurrentincluqueue <Object> (); // 2. use hashmap to mount all worker objects private HashMap <String, Thread> workers = new HashMap <> (); // 3. use a container to load the private ConcurrentHashMap <String, Object> resultMap = new ConcurrentHashMap <> (); // 4. constructor public Master (Worker worker, int workerCount) {// each worker object must be referenced by a Master. workqueue is used for tasks. The resultMap is used to submit worker jobs. setWorkerQueue (workqueue); worker. setResultMap (resultMap); for (int I = 0; I <workerCount; I ++) {// key indicates the name of each worker, and value indicates the workers of the thread execution object. put ("task" + Integer. toString (I), new Thread (worker) ;}// 5. submit the public void submit (Object object) {this. workqueue. add (object);} // 6. execute all worker jobs public void execute () {for (Map. entry <String, Thread> work: workers. entrySet () {work. getValue (). Start () ;}// determine whether to end public boolean isExist () {for (Map. entry <String, Thread> exist: workers. entrySet () {if (exist. getValue (). getState ()! = Thread. state. TERMINATED) {return false ;}} return true;} // return result set public long getResult () {long result = 0L; for (Map. entry <String, Object> re: resultMap. entrySet () {result + = (Integer) re. getValue () ;}return result ;}}
Public class Worker implements Runnable {private concurrent1_queue <Object> workerQueue; private ConcurrentHashMap <String, Object> resultMap; public void setWorkerQueue (concurrent1_queue <Object> workerQueue) {this. workerQueue = workerQueue;} public void setResultMap (ConcurrentHashMap <String, Object> resultMap) {this. resultMap = resultMap;} @ Override public void run () {while (true) {Task task Task = (Task) workerQueue. poll (); if (task = null) break; // indicates that no task is available. // process the Business Object result = handle (task ); // put the result set in resultMap. put (Integer. toString (task. getId (), result) ;}} private Object handle (Task task) {Object result = null; try {Thread. sleep (500); result = task. getPrice ();} catch (InterruptedException e) {e. printStackTrace ();} return result ;}}
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;    }}

 

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.