Master-worker mode in multi-threading of Java

Source: Internet
Author: User

The benefit of this pattern is that the large task is disassembled into several small tasks and executed in parallel, thereby increasing system Throughput.
Defines the worker process, which is responsible for handling the actual Task.
/ * Specific Work Object */
Static abstract classworker<T, R>ImplementsRunnable {
private static FinalUtilslogLG= Utilslog.GetLogger(Worker.class);
protectedqueue<T>WorkQueue;//hold master's Task queue
protectedMap<string, R>Resultmap;//for Storing the result set, key is the unique identifier corresponding to the task

public voidSetworkqueue(queue<T> WorkQueue) {
this.WorkQueue= WorkQueue;
}

public voidSetresultmap(map<string, R> Resultmap) {
this.Resultmap= Resultmap;
}

public AbstractRHandler(TEntity;

@Override
public voidRun() {
while(true) {
TChildwork =WorkQueue. Poll ();
if(childwork = =NULL) {
LG. E ("there are no tasks waiting to be executed in the queue");
break ;
}
//handling Subtasks
Rresult = Handler (childwork);
Resultmap. Put (Integer.toString(childwork.hashcode ()), Result;
}
}
}
Availabilitythe Master process, which is responsible for receiving and assigning tasks. Master returns the result set as soon as the task is submitted, asGetresultmap belongs to reference delivery, so the modification of the property Resultmap is synchronized to the business Layer.
public Static Classmaster<T, R> {
private static FinalUtilslogLG= Utilslog.GetLogger(Master.class);
protectedqueue<T>WorkQueue;//for Storing task sets
protectedMap<string, Thread>Threadmap;//store The set of threads that perform tasks
protectedMap<string, R>Resultmap;//store Related Results

@TargetApi(build.version_codes.LOLLIPOP)
publicMaster (worker<T, R> Work, intThreadcount) {
WorkQueue=Newconcurrentlinkeddeque<T> ();
Threadmap=NewHashmap<> ();
Resultmap=NewHashmap<> ();

Work.setworkqueue (WorkQueue);
Work.setresultmap (Resultmap);
for(inti =0; I < ThreadCount; I++) {
Threadmap. Put (Integer.toString(i), NewThread (work, "thread Tag with"+ Integer.toString(i)));
}
}

//whether all sub-tasks are over
public BooleanIscomplete() {
for(map.entry<string, Thread> entry:Threadmap. EntrySet ()) {
if(entry.getvalue (). getState ()! = Thread.state.TERMINATED) {
return false;
}
}
return true;
}

publicMap<string, R>Getresultmap() {
returnResultmap;
}

publicMasterAddJob(TJob) {
WorkQueue. Add (job);
return this;
}

public voidExecute() {
for(map.entry<string, Thread> entry:Threadmap. EntrySet ()) {
Entry.getvalue (). Start ();
}
}
}
in theThe business layer invocation scenario is as follows,
Master<integer, Integer> master =NewMaster<> (NewWorker<integer, Integer> () {
@Override
publicIntegerHandler(Integer Entity) {
intMax = -, Min =0;
Utilsthread.sleepignoreinteruptedexception(NewRandom (). nextint (max)% (max-min +1) + Min);//random Simulation time-consuming operation
LG. E ("execute Handler program with value:"+ Entity);
returnEntity * Entity;
}
}, 3);
intJobcount =Ten;//number of tasks
for(inti =0; I < Jobcount; I++) {
Master.addjob (i);
}
Master.execute ();

Map<string, integer> Resultmap = Master.getresultmap ();
while(true) {
intResultmapsize = Resultmap.size ();
//lg.e ("the amount of data in a parallel execution result set:" + resultmapsize);//here Resultmap holds a reference to the result set in master, so the continuous refresh of results in the course of continuous execution of the thread will result in a change in value Here.
if(master.iscomplete ()) {
Break
}
}
execution results are as follows,

In addition, it should be noted that in implementinghandler method, The data written to the result set is delayed,This requires extra attention in development, so be sure to use Master.Iscomplete Judging task completion status.





From for Notes (Wiz)

Master-worker mode in multi-threading of Java

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.