Design Pattern: worker thread mode

Source: Internet
Author: User

Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 292 & extra = Page % 3d1

The worker thread mode is like Producer consumer Mode The request behavior is like Command mode .

The producer consumer mode focuses on the production and consumption of the product. It does not discuss how the product is consumed.

If your product is a request, the consumer executes the request method specified in the request after obtaining the request, that is, the command mode is used, and your Request Buffer also manages the consumer, it means the worker thread mode.

On the sequence divisor, we can see that the worker thread shows both the producer consumer mode and the command mode:

A channel class implemented using Java is as follows:

    • Channel. Java

Import java. util. Collections list;

Public class channel {
Private Response List requests;
Private workerthread [] workerthreads;

Public channel (INT threadnumber ){
Requests = new queue list ();
Workerthreads = new workerthread [threadnumber];
For (INT I = 0; I <workerthreads. Size (); I ++ ){
Workerthreads= New workerthread ();
Workerthreads. Start ();
}
}

Public synchronized void putrequest (request) {
while (requests. size ()> = 2) {// Capacity Limit: 2
try {
wait ();
}< br> catch (interruptedexception E) {}< BR >}

Requests. addlast (request );
Policyall ();
}

Public synchronized request getproduct (){
While (requests. Size () <= 0 ){
Try {
Wait ();
}
Catch (interruptedexception e ){}
}

Request request = (request) requests. removefirst ();
Policyall ();

Return request;
}
}

Command mode used between the request class and workerthread class:

    • Request. Java

Public class request (){
//....

Public void execute (){
// Do some work ....
}
}

    • Workerthread. Java

Public class workerthread extends thread {
//...

Public void run (){
While (true ){
Request request = channel. getrequest ();
Request.exe cute ();
}
}
}

in terms of behavior, workerthread performs a request. If no request is sent, all workerthreads will wait, wait until new jobs come in and notify them of the work to be done by the workerthread that gets the request, which is directly defined in execute.

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.