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