当前位置: 首页 > news >正文

Worker-Thread设计模式

    Worker-Thread模式类似于工厂流水线,有时也称为流水线设计模式。线程池在某种意义上也算是Worker-Thread模式的一种实现,线程池初始化时创建线程类似于在流水线等待工作的工人,提交给线程池的Runnable接口类似于需要加工的产品,Runnable的run方法相当于组装该产品的说明书。Worker-Thread模式需要如下几个角色:

  • 流水线工人:对传送带上的产品进行加工

  • 流水线传送带:用于传送来自上游的产品

  • 产品组装说明书:用于说明该产品如何组装

     Worker-Thread模式中生产线保存了在处理中的产品,并且是启动生产线的线程后,生产线启动若干数量的流水线工人线程 ,生产线聚合了产品和工人。生产者消费者模式是单纯的依赖关系,生产者和消费者都依赖产品队列,生产者和消费者是相互不知道。  

示例代码如下:

public abstract class InstructionBook {
protected abstract void firstProcess();
protected abstract void secondProcess();public final void create() {
this.firstProcess();
this.secondProcess();
}}
public class Production extends InstructionBook{
private final int productId;public Production(int productionId) {
this.productId=productionId;
}public int getProductionId() {
return this.productId;
}@Override
protected void firstProcess() {
System.out.println("execute the "+this.productId+" first process");
}@Override
protected void secondProcess() {
System.out.println("execute the "+this.productId+" second process");
}}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;public class Worker extends Thread{
private final ProductionChannel channel;public Worker(String workerName, ProductionChannel channel) {
super(workerName);
this.channel=channel;
}public void run() {
while(true) {
try {
Production production=this.channel.takeProduction();
System.out.println(getName()+ " process the "+production.getProductionId());
production.create();
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}}
import java.util.ArrayList;public class ProductionChannel {
private final ArrayList<Production> productionQueue=new ArrayList<>();
private int total=20;
private final Worker[] workers;public ProductionChannel(int workerSize) {
this.workers=new Worker[workerSize];
for(int i=0;i<workerSize;i++) {
workers[i]=new Worker("Worker-"+i,this);
workers[i].start();
}
}public void offerProduction(Production production) {
synchronized(this) {
while(total<=this.productionQueue.size()) {
try {
System.out.println("processing production id="+production.getProductionId()+" , in waiting state");
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("processing production id="+production.getProductionId());
this.productionQueue.add(production);
this.notifyAll();
}
}public Production takeProduction() {
synchronized(this) {
while(this.productionQueue.size()<=0) {
try {
System.out.println("processing to fetch production, while in waiting state");
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notifyAll();
Production p=this.productionQueue.get(0);
this.productionQueue.remove(0);
return p;
}
}}
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;public class WTtest {public static void main(String[] args) {
ProductionChannel channel=new ProductionChannel(5);
AtomicInteger pid=new AtomicInteger();
IntStream.range(1, 8).forEach(i->new Thread(()->{
// while(true) {
channel.offerProduction(new Production(pid.getAndIncrement()));
try {
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(5));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
}).start());
}}

http://www.lryc.cn/news/262310.html

相关文章:

  • npm 安装包遇到问题的常用脚本(RequestError: socket hang up)
  • 活动 | Mint Blockchain 将于 2024 年 1 月 10 号启动 MintPass 限时铸造活动
  • Android动画(四)——属性动画ValueAnimator的妙用
  • C语言飞机大战
  • js 原型 和 原型链
  • 如何利用SD-WAN节省运维成本和简化运维工作?
  • 在工作中使用CHAT提高效率
  • Maven 项目的三种打包方式与 pom.xml 文件中项目描述
  • 【普中】基于51单片机简易计算器数码管显示设计( proteus仿真+程序+实物演示+讲解视频)
  • 【Android】DeepLink
  • 微服务Redis-Session共享登录状态
  • 30道C++ 基础高频题整理(附答案背诵版)
  • 【Spark面试】Spark面试题答案
  • Axure的动态面板
  • 【STM32】STM32学习笔记-对射式红外传感器计次 旋转编码器计次(12)
  • 后端项目操作数据库-中枢组件Service调用Mapper实现增删改查-实例
  • kafka学习笔记--节点的服役与退役
  • 2023-12-16:用go语言,给定整数数组arr,求删除任一元素后, 新数组中长度为k的子数组累加和的最大值。 来自字节。
  • libxls - 编译
  • 自建私有git进行项目发布
  • 华为HCIP认证H12-821题库上
  • Web安全漏洞分析—文件包含
  • C++入门【9-C++循环】
  • Python3 数字(Number) ----20231215
  • PyQt6 QToolBar工具栏控件
  • nodejs+vue+微信小程序+python+PHP基于大数据的银行信用卡用户的数仓系统的设计与实现-计算机毕业设计推荐
  • EMC RI/CI测试方案助您对抗电磁设备干扰!
  • 【机器学习】数据降维
  • vue3路由跳转及传参
  • cesium 自定义贴图,shadertoy移植教程。