多线程程序设计学习(9)worker pattern模式
Worker pattern[工作模式]
一:Worker pattern的参与者
--->Client(委托人线程)
--->Channel(通道,里边有,存放请求的队列)
--->Request(工作内容的包装)
--->Worker(工人线程)
二:Worker pattern模式什么时候使用
--->类似生产者消费者
三:Worker pattern思考
四进阶说明
--->工作线程取出请求内容包装后,根据多态,不同的请求执行不同的业务方法
Request接口
package com.yeepay.sxf.thread7;
/**
* 抽象请求
* @author sxf
*
*/
public interface Request {
//定义的抽象方法
public void eat();
}
不同Request接口的实现类
人的请求包装
package com.yeepay.sxf.thread7;
/**
* 人的请求
* @author sxf
*
*/
public class PersonRequest implements Request{
//姓名
private String name;
//事物
private String food; public PersonRequest(String name, String food) {
super();
this.name = name;
this.food = food;
} @Override
public void eat() { System.out.println("PersonRequest.eat()"+name+"吃掉"+food);
fell();
} public void fell(){
System.out.println("PersonRequest.fell()"+"我吃饱了");
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getFood() {
return food;
} public void setFood(String food) {
this.food = food;
} }
猫的请求包装
package com.yeepay.sxf.thread7;
/**
* 猫的请求
* @author sxf
*
*/
public class CatRequest implements Request{
//姓名
private String name;
//年龄
private int age; public CatRequest(String name, int age) {
super();
this.name = name;
this.age = age;
} @Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("CatRequest.eat()"+name+"有"+age+"月大"); } public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} }
模拟工作线程取出请求,执行不同的业务
package com.yeepay.sxf.thread7;
/**
* 工作线程
* @author sxf
*
*/
public class Test { public static void main(String[] args) { //模拟线程取出不同的的请求包装
Request peRequest=new PersonRequest("尚晓飞", "汉堡");
Request catRequest=new CatRequest("黑猫警长", 3);
//不同的请求包装利用多态调用方法,实现不同的业务
peRequest.eat();
catRequest.eat();
} }
打印结果
PersonRequest.eat()尚晓飞吃掉汉堡
PersonRequest.fell()我吃饱了
CatRequest.eat()黑猫警长有3月大
多线程程序设计学习(9)worker pattern模式的更多相关文章
- 多线程程序设计学习(3)immutable pattern模式
Immutable pattern[坚不可摧模式] 一:immutable pattern的参与者--->immutable(不变的)参与者 1.1:immutable参与者是一个 ...
- 多线程程序设计学习(2)之single threaded execution pattern
Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...
- 多线程程序设计学习(6)Producer-Consumer模式
Producer-Consumer[生产消费者模式]一:Producer-Consumer pattern的参与者--->产品(蛋糕)--->通道(传递蛋糕的桌子)--->生产者线程 ...
- 多线程程序设计学习(5)balking模式和timed模式
Balking[返回模式]timed[超时模式]一:balking pattern的参与者--->GuardedObject(被警戒的对象) --->该模式的角色:模拟修改警戒对象的线程, ...
- 多线程程序设计学习(10)Future pattern
Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...
- 多线程程序设计学习(7)read-write lock pattern
Read-Write Lock Pattern[读写]一:Read-Write Lock Pattern的参与者--->读写锁--->数据(共享资源)--->读线程--->写线 ...
- 多线程程序设计学习(4)guarded suspension模式
Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者 1.1该 ...
- 多线程程序设计学习(13)Active Object pattern
Active Object[接收异步消息的对象] 一:Active Object的参与者--->客户端线程(发起某种操作请求处理)--->代理角色(工头)--->实际执行者(工人)- ...
- 多线程程序设计学习(12)Thread-soecific storage pattern
Thread-Specific-Storage[线程保管箱] 一:Thread-Specific Storage的参与者--->记录日志的线程(ClientThread)--->负责获取不 ...
随机推荐
- 【WPF】Application应用程序启动
wpf应用程序在启动的时候会自动创建Main函数并调用Application实例的run(),从而启动Application进程.Main函数在一个App.g.cs文件中,App.g.cs文件的位置在 ...
- Header:请求头参数详解
Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html,application/json Accept-Charset 浏览器 ...
- 1037. Magic Coupon (25)
#include<iostream> #include<vector> #include<stdio.h> #include<algorithm> us ...
- 1027 Colors in Mars (20)
#include <stdio.h> #include <map> using namespace std; int main() { int R,G,B,i; map< ...
- 《ENVI下遥感影像自然真彩色合成方法》——TM、spot5
来源:http://blog.sina.com.cn/s/blog_764b1e9d0100tz4f.html#bsh-73-375271630
- php的异步框架
swoole目前已被多家移动互联网.物联网.网络游戏.手机游戏企业使用,替代了C++.Java等复杂编程语言来实现网络服务器程序. 使用PHP+Swoole,开发效率可以大大提升.官方提供了基于swo ...
- ARC工程中添加非ARC文件
转载自:http://blog.csdn.net/zhenweicao/article/details/16988543 分类: IOS2013-11-27 17:02 626人阅读 评论(0) 收藏 ...
- 如何在windows下安装GIT
如何在windows下安装GIT 分步阅读 Git是一个免费的.开源的版本控制软件.在Windows上安装git,一般为msysgit,官方下载地址为 http://code.google.com/p ...
- iOS的layoutSubviews和drawRect方法何时调用
layoutSubviews在以下情况下会被调用: 1.init初始化不会触发layoutSubviews.2.addSubview会触发layoutSubviews.3.设置view的Frame会触 ...
- linux编程之线性表
#include"stdio.h" #define MAX 100 typedef struct List{ int length; int num[MAX]; }List_seq ...