Java Thread Series (ix) Master-worker mode
The Master-worker mode is a common parallel design pattern.
First, the core idea of Master-worker model
The Master-worker system consists of two roles, Master and Worker,master are responsible for receiving and assigning tasks, and the Worker is responsible for processing subtasks. Master is also responsible for overseeing the progress of tasks and the health status of workers during task processing, and master will receive client-submitted tasks and summarize the progress of the tasks to the client. The role relationships are as follows:
Second, the realization of Master-worker
(1) Master
import Java.util.HashMap;import Java.util.Map;import Java.util.concurrent.ConcurrentHashMap;import Java.util.concurrent.ConcurrentLinkedDeque; Public classMaster {//1. There should be a container to hold the task list, which needs to support high concurrency operations PrivateConcurrentlinkeddeque<task> Taskqueue =NewConcurrentlinkeddeque<task> ();//2. There should be a container to store the worker Privatehashmap<string, thread> workers =NewHashmap<string, thread> ();//3. There should be a container to hold the result set, which needs to support high concurrency operations Privateconcurrenthashmap<string, object> resultmap =NewConcurrenthashmap<string, object> ();//4. Constructors Public Master(Worker worker,intThreadCount) {//Pass task List and result set to workerWorker.Settaskqueue(Taskqueue); Worker.Setresultmap(RESULTMAP);//Initialize Worder list for(inti =0; i < ThreadCount; i++) {workers.put("worker-"+ I,NewThread (worker)); } } Public Master(Worker worker) { This(worker, Runtime.)GetRuntime().availableprocessors()); }//5. Submitting a task Public void Submit(Task Task) {Taskqueue.Add(Task); }//6. Execute method to open all threads Public void Execute() { for(Map.Entry<string, thread> me:workers.EntrySet()) {me.GetValue().Start(); } }//7. Determine if execution is complete Public Boolean Iscomplete() { for(Map.Entry<string, thread> me:workers.EntrySet()) {if(Me.)GetValue().getState()! = Thread. State.TERMINATED)return false; }return true; }//8. Working with result sets Public int GetResult() {intRET =0; for(Map.Entry<string, object> Me:resultmap.EntrySet()) {ret + = (int) Me.GetValue(); }returnRet }}
(2) Worker
import Java.util.concurrent.ConcurrentHashMap;import Java.util.concurrent.ConcurrentLinkedDeque; Public classWorkerImplementsRunnable {PrivateConcurrentlinkeddeque<task> Taskqueue;PrivateConcurrenthashmap<string, object> Resultmap;@Override Public void Run() { while(true) {Task task = Taskqueue.Poll();if(Task = =NULL) Break;Try{Thread.Sleep( -); }Catch(Interruptedexception e) {e.Printstacktrace(); }//return result setResultmap.put(Integer.toString(Task.getId()),Handle(Task)); } }PrivateObjectHandle(Task Task) {returnTask.GetCount(); } Public void Settaskqueue(concurrentlinkeddeque<task> taskqueue) { This.Taskqueue= Taskqueue; } Public void Setresultmap(concurrenthashmap<string, object> resultmap) { This.Resultmap= Resultmap; }}
(3) Task
Public classTask {Private intIdPrivateString name;Private intCount Public Task() {} Public Task(intID, String name,intCount) { This.ID= ID; This.name= name; This.Count= Count; } Public int getId() {returnId } Public void setId(intID) { This.ID= ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This.name= name; } Public int GetCount() {returnCount } Public void SetCount(intCount) { This.Count= Count; }@Override PublicStringtoString() {return "task{"+"Id="+ ID +", Name= '"+ name +' \ '+", count="+ Count + '} '; }}
(4) test
Master Master =New Master(New Worker(),1); for(inti =1; I <= -; i++) {Master.Submit(New TaskI"task-"+ I, i));} Master.Execute();LongT1 = System.Currenttimemillis(); while(true) {if(Master.Iscomplete()) {Longt = System.Currenttimemillis()-T1; System.out.printf("Execution results:%s; execution time:%s", Master.GetResult(), t); Break; }}
Record a little bit every day. Content may not be important, but habits are important!
Java Thread Series (ix) Master-worker mode