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模式的更多相关文章

  1. 多线程程序设计学习(3)immutable pattern模式

    Immutable pattern[坚不可摧模式] 一:immutable pattern的参与者--->immutable(不变的)参与者        1.1:immutable参与者是一个 ...

  2. 多线程程序设计学习(2)之single threaded execution pattern

    Single Threaded Execution Pattern[独木桥模式] 一:single threaded execution pattern的参与者--->SharedResourc ...

  3. 多线程程序设计学习(6)Producer-Consumer模式

    Producer-Consumer[生产消费者模式]一:Producer-Consumer pattern的参与者--->产品(蛋糕)--->通道(传递蛋糕的桌子)--->生产者线程 ...

  4. 多线程程序设计学习(5)balking模式和timed模式

    Balking[返回模式]timed[超时模式]一:balking pattern的参与者--->GuardedObject(被警戒的对象) --->该模式的角色:模拟修改警戒对象的线程, ...

  5. 多线程程序设计学习(10)Future pattern

    Future pattern[订单取货模式] 一:Future pattern的参与者--->Client(客户需求)--->Host(蛋糕门店)--->Data(票据和蛋糕的接口) ...

  6. 多线程程序设计学习(7)read-write lock pattern

    Read-Write Lock Pattern[读写]一:Read-Write Lock Pattern的参与者--->读写锁--->数据(共享资源)--->读线程--->写线 ...

  7. 多线程程序设计学习(4)guarded suspension模式

    Guarded Suspension[生产消费者模式] 一:guarded suspension的参与者--->guardedObject(被防卫)参与者                1.1该 ...

  8. 多线程程序设计学习(13)Active Object pattern

    Active Object[接收异步消息的对象] 一:Active Object的参与者--->客户端线程(发起某种操作请求处理)--->代理角色(工头)--->实际执行者(工人)- ...

  9. 多线程程序设计学习(12)Thread-soecific storage pattern

    Thread-Specific-Storage[线程保管箱] 一:Thread-Specific Storage的参与者--->记录日志的线程(ClientThread)--->负责获取不 ...

随机推荐

  1. c语言文件操作函数详解

    一.文件操作注意点: 1 打开文件时,如果打开方式加“+”,表示该文件可以“写” ; 2 退出程序一般用exit函数,正常退出参数为0,非正常退出参数为正零值 ; 3 文件的读写操作:按字符.字符串. ...

  2. 【Entity Framework 7】 完全不一样的玩法

    http://www.cnblogs.com/n-pei/p/4274907.html

  3. 微软职位内部推荐-This Job is no longer available.

    微软近期Open的职位: 如果你想试试这个职位,请跟我联系,我是微软的员工,可以做内部推荐.发你的中英文简历到我的邮箱:Nicholas.lu.mail(at)gmail.com

  4. Jplayer(转)

    Jplayer必须要加载 1.样式  jplayer.blue.monday.css 2.jq jquery.1.6.2.min.js 当前最新版本为1.6.2 3.jplayer的js jquery ...

  5. 1043: [HAOI2008]下落的圆盘 - BZOJ

    Description有n个圆盘从天而降,后面落下的可以盖住前面的.求最后形成的封闭区域的周长.看下面这副图, 所有的红色线条的总长度即为所求.Input n ri xi y1 ... rn xn y ...

  6. nginx+ tomcat集群+动静资源分离

    不知道为什么这个随便删不掉,写了也值显示一半一半不显示, 我把重新写了一遍: nginx + tomcat集群和动静资源分离

  7. EasyUI Datagrid 取编辑修改后的内容

    <script type="text/javascript"> $(function () { $('#tt').datagrid({ iconCls: 'icon-e ...

  8. DeepFace--Facebook的人脸识别(转)

    DeepFace基本框架 人脸识别的基本流程是: detect -> aligh -> represent -> classify 人脸对齐流程 分为如下几步: a. 人脸检测,使用 ...

  9. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  10. maven更新总结与tomcat发布方法总结

    这些天来一直为不能直接把项目实时的发布到tomcat而费心思,项目使用了maven来组织,编译和运行,而maven插件的安装曾经有些问题,为此怀疑不能发布项目到tomcat是因为maven有问题,为些 ...