Multithreaded Programming Learning (9) Worker pattern mode

Source: Internet
Author: User

Worker pattern[Working mode]
One: Participants in Worker pattern
--->client (principal thread)
--->channel (channel, inside, storage queue for request)
--->request (packaging of work contents)
--->worker (worker thread)


Two: When the Worker pattern pattern is used
---> Similar producer consumers


Three: Worker pattern thinking


Four step instructions
---> Worker threads take out the requested content after wrapping, depending on polymorphism, different requests execute different business methods

Request interface

1  PackageCom.yeepay.sxf.thread7;2 /**3 * Abstract Request4  * @authorSXF5  *6  */7  Public InterfaceRequest {8     //the abstract method of the definition9      Public voideat ();Ten}
View Code

Implementation classes for different request interfaces

Person's request packing

1  PackageCom.yeepay.sxf.thread7;2 /**3 * person's request4  * @authorSXF5  *6  */7  Public classPersonrequestImplementsrequest{8     //name9     PrivateString name;Ten     //things One     PrivateString food; A      -      -      Publicpersonrequest (string name, string food) { the         Super(); -          This. Name =name; -          This. Food =Food ; -     } +  -  + @Override A      Public voideat () { at          -System.out.println ("Personrequest.eat ()" +name+ "eat" +Food ); - fell (); -     } -  -      Public voidfell () { inSystem.out.println ("Personrequest.fell ()" + "I'm Fed"); -     } to      +      -      PublicString GetName () { the         returnname; *     } $ Panax Notoginseng  -      Public voidsetName (String name) { the          This. Name =name; +     } A  the  +      PublicString Getfood () { -         returnFood ; $     } $  -  -      Public voidSetfood (String food) { the          This. Food =Food ; -     }Wuyi      the      -  Wu}
View Code

Cat's request packing

1  PackageCom.yeepay.sxf.thread7;2 /**3 * Cat's request4  * @authorSXF5  *6  */7  Public classCatrequestImplementsrequest{8     //name9     PrivateString name;Ten     //Age One     Private intAge ; A      -      -      PublicCatrequest (String name,intAge ) { the         Super(); -          This. Name =name; -          This. Age =Age ; -     } +  -  + @Override A      Public voideat () { at         //TODO auto-generated Method Stub -System.out.println ("Catrequest.eat ()" +name+ "has" +age+ "month Big"); -          -     } -  -  in      PublicString GetName () { -         returnname; to     } +  -  the      Public voidsetName (String name) { *          This. Name =name; $     }Panax Notoginseng  -  the      Public intGetage () { +         returnAge ; A     } the  +  -      Public voidSetage (intAge ) { $          This. Age =Age ; $     } -      -      the  -}
View Code

Simulate worker threads take out requests, perform different business

1  PackageCom.yeepay.sxf.thread7;2 /**3 * Worker Threads4  * @authorSXF5  *6  */7  Public classTest {8     9      Public Static voidMain (string[] args) {Ten  One         //simulate thread out of different request wrapper ARequest perequest=NewPersonrequest ("Shang", "Hamburg"); -Request catrequest=NewCatrequest ("Black Cat Sheriff", 3); -         //different request wrappers utilize polymorphic call methods to achieve different business the perequest.eat (); - catrequest.eat (); -     } -  +}
View Code

Print results

Personrequest.eat () Shang Eat burger
Personrequest.fell () I'm full.
Catrequest.eat () Black Cat Sheriff has March big

Multithreaded Programming Learning (9) Worker pattern mode

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.