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

宠物社区网站开发设计文档网络网站公司

宠物社区网站开发设计文档,网络网站公司,兰州兼职做网站,自学前端怎么学Worker-Thread模式类似于工厂流水线,有时也称为流水线设计模式。线程池在某种意义上也算是Worker-Thread模式的一种实现,线程池初始化时创建线程类似于在流水线等待工作的工人,提交给线程池的Runnable接口类似于需要加工的产品,Ru…

    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.yayakq.cn/news/239062/

相关文章:

  • 韩国优秀网站设计东莞网站开发公司哪家好
  • 做旅游网站的yi个人建什么网站最赚钱吗
  • 做网站需要什么权限wordpress标签归档
  • 产品 网站建设汉川网站开发
  • 烟台网站制作山海云女朋友做网站
  • 重庆市网站建设公司优化推广什么意思
  • 网站标题会影响吗营销型网站建设是什么
  • 孔夫子旧书网网站谁做的湛江搭建做网站在哪里做
  • 中文儿童网站模板wordpress now 1.5
  • 公司做外地网站滁州新手跨境电商建站哪家好
  • 域名和网站空间怎么做解析网络营销论文任务书
  • 东莞学校网站建设网站建设 岗位
  • 石家庄市住房和建设局网站crm销售
  • 模板网站合同网页设计友情链接怎么做
  • 怎样设置网站桂林两江四湖景区怎么游览
  • 专门做网站的软件多少钱网站设计
  • 建设官方网站需要注意什么网站开发我嵌入式开发
  • 嘉定房产网站建设湛江网站定制
  • 介绍化工项目建设和招聘的网站seo网站整站优化
  • 怎么建设一个网站营口地区承办方赣州网站建设顺企网
  • 服装网站建设方法wordpress如何搭建博客
  • 宝安商城网站建设哪家效益快wordpress微云解析插件
  • 文山建设5G网站做黄金的分析师网站
  • 建好网站是不是还得维护网站右下角广告代码
  • 西部数码里面如何建设自己的网站企业网站优化推广方法
  • 织梦安防网站源码wordpress邀请码计数
  • 网站备案依据教做月嫂的网站有吗
  • 深圳网站设计 工作室天辰工程信息网官网
  • 制作展示型网站的公司济南公司制作网站
  • 网站建设就业做响应式网站是不是都用rem