Java thread model Master-Worker

Source: Internet
Author: User

This model is one of the most commonly used parallel modes. For more information about Nginx source code, visit this blog to learn about http://blog.csdn.net/marcky/article/details/6014733. From an ideological point of view, it mainly consists of two types of processes: Master process and Worker process. The Master process is responsible for receiving and allocating tasks, and the Worker process is responsible for processing subtasks. After the Worker completes processing the subtasks, the results are returned to the Master process for induction and summarization by the Master process, the final result is displayed. See the figure for the specific process.


This mode breaks down a large task into several small tasks for execution. It is suitable for some time-consuming tasks and can improve the system throughput.

A relatively complete model should have the following functions


Based on the column child in the java performance optimization book, a simple Master-Worker mode is implemented above. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PHByZSBjbGFzcz0 = "brush: java;"> package com. thread; import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. queue; import java. util. set; import java. util. concurrent. concurrentHashMap; import java. util. concurrent. concurrentincludeque; public class Master_Worker {public static void main (String args []) {long start = System. currentTimeMillis (); Master_Worker master_Worker = new Master_Worker (new PlusWorker (), 11); for (int I = 0; I <100; I ++) {master_Worker.submit (I );} master_Worker.execute (); int re = 0; Map Result_Map = master_Worker.getResultMap (); while (result_Map.size ()> 0 |! Master_Worker.isComplete () {Set KeysSet = result_Map.keySet (); String keyString = null; for (String string: keysSet) {keyString = string; break;} Integer I = null; if (keyString! = Null) {I = (Integer) result_Map.get (keyString);} if (I! = Null) {re + = I;} if (keyString! = Null) {result_Map.remove (keyString) ;}} long end = System. currentTimeMillis (); System. out. println ("Result:" + re + "-between execution" + (end-start); int sum = 0; start = System. currentTimeMillis (); for (int I = 1; I <= 100; I ++) {sum + = I * I; try {Thread. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}}end = System. currentTimeMillis (); System. out. println ("Result:" + sum + "-between execution" + (end-start);} // Job Queue protected Queue WorkerQueue = new concurrentincludeque <> (); // Worker Process queue protected Map ThreadMap = new HashMap <> (); // The subtask processing result set protected Map ResultMap = new ConcurrentHashMap <> (); // whether all subtasks have ended the public boolean isComplete () {for (Map. Entry Entry: threadMap. entrySet () {if (entry. getValue (). getState ()! = Thread. state. TERMINATED) {return false ;}} return true;} // Master construction. A Worker process logic is required, and the number of Worker processes required is public Master_Worker (Worker woker, int countWorker) {woker. setWorkQueue (workerQueue); woker. setResultMap (resultMap); for (int I = 0; I <countWorker; I ++) {threadMap. put (Integer. toString (I), new Thread (woker, Integer. toString (I); }}// return the subtask result set public Map GetResultMap () {return resultMap;} // submit the public void submit (Object job) {workerQueue. add (job);} public void execute () {for (Map. Entry Entry: threadMap. entrySet () {if (entry. getValue (). getState ()! = Thread. State. TERMINATED) {entry. getValue (). start () ;}}} class Worker implements Runnable {// task Queue, used to obtain the subtask protected Queue WorkQueue; // The subtask processing result set protected Map ResultMap; public void setWorkQueue (Queue WorkQueue) {this. workQueue = workQueue;} public void setResultMap (Map ResultMap) {this. resultMap = resultMap;} // sub-task processing logic. Implement the specific public Object handle (Object input) in the subclass) {/* Here you can write what you want to do */return input;} @ Overridepublic void run () {// TODO Auto-generated method stubwhile (true) {// obtain the subtask Object inputObject = workQueue. poll (); if (inputObject = null) {break;} // process the subtask Object reObject = handle (inputObject); resultMap. put (Integer. toString (inputObject. hashCode (), reObject) ;}}/ ** extends your own class **/class PlusWorker extends Worker {@ Overridepublic Object handle (Object input) {// TODO Auto-generated method stub // You can implement your own business logic here. Here, I sleep the Thread for 100 milliseconds and run try {Thread in a simulated task. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} Integer I = (Integer) input; return I * I ;}}
Most of them refer to the java performance optimization book and add their own adaptation and introduction.

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.