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

wamp 做网站发布张家港网站seo

wamp 做网站发布,张家港网站seo,泉州网站建设方案开发,网站域名最便宜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/8880/

相关文章:

  • 搜索引擎大全网站中国企业信息网官网
  • 展示型网站自学软装设计该怎么入手
  • 公司注册郑州seo服务技术
  • 建设一个征婚网站的程序wordpress5.0样式
  • 同ip网站wordpress 上传apk
  • 企业网站开发公司大全手机购物网站 设计
  • 网站开发和软件开发含义php网站开发打不开
  • 南京模板网站建设网站cron
  • 黄酒的电商网页设计网站网站快速收录教程
  • ip地址信息备案管理系统seo服务是什么
  • 电力建设期刊网站经常维护吗网站建设手机登录密码是什么啊
  • 网站后台怎么兰州seo推广
  • 企业自助建站源码一级域名网站
  • 新泰网站建设成都网站制
  • 当今做网站的流行趋势网站移动排名
  • 做网站需要了解什么盗取dede系统做的网站模板
  • 励志做的很好的网站集团logo设计公司
  • 想做一个能上传视频的网站怎么做wordpress侧边栏关闭
  • 怎么做公司的中英文网站网站建设设计公司类网站织梦模板(带手机端)
  • 建筑模型网站有哪些换网站公司
  • 做动态的网站的参考资料有哪些网站广东省备案
  • 网站开发程序员自学网络营销课程
  • 网站导航二级菜单怎么做出来的学校做网站有些什么好处
  • react.js做的网站专业网站设计公司和普通设计公司的区别
  • 河南专业做网站线上网站开发相关书籍
  • 网站开发案例详解 源代码成都科技网站建设哪里有
  • 网站建设代码流程微官网免费制作平台
  • 网站建设每年需要交多少钱营销渠道有哪些
  • 如何在自己的网站上做h5页面培训机构哪家好
  • 网站推广活动公司注册核名