Worker-Thread设计模式
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args){
ProductionChannelTest.test();
}
}
abstract class InstructionBook{
public final void create(){
this.firstProcess();
this.sedoncProcess();
}
protected abstract void firstProcess();
protected abstract void sedoncProcess();
}
class Production extends InstructionBook{
private final int prodID;
public Production(int prodID) {
this.prodID = prodID;
}
@Override
protected void firstProcess() {
System.out.println("execute the "+prodID+" first process");
}
@Override
protected void sedoncProcess() {
System.out.println("execute the "+prodID+" second process");
}
}
class Worker extends Thread{
private final ProductionChannel channel;
private final static Random random = new Random(System.currentTimeMillis());
public Worker(String name, ProductionChannel channel) {
super(name);
this.channel = channel;
}
@Override
public void run() {
while (true) {
try{
Production production = channel.takeProduction();
System.out.println(getName()+" process the "+production);
production.create();
TimeUnit.SECONDS.sleep(random.nextInt(10));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class ProductionChannel{
private final static int MAX_PROD = 100;
private final Production[] productionQueue;
private int tail; //队列尾
private int head; //队列头
private int total; //当前流水有多少个待加工的产品
private final Worker[] workers;
public ProductionChannel(int workSize) {
this.workers=new Worker[workSize];
this.productionQueue=new Production[MAX_PROD];
for (int i = 0; i < workSize; i++) {
workers[i]=new Worker("Worker-"+i,this);
workers[i].start();
}
}
public void offerProduction(Production production){
synchronized (this) {
while (total >= productionQueue.length) {
try{
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
productionQueue[tail]=production;
tail=(tail+1)%productionQueue.length;
total++;
this.notifyAll();
}
}
public Production takeProduction(){
synchronized (this) {
while (total <= 0) {
try{
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Production production = productionQueue[head];
head = (head+1)%productionQueue.length;
total--;
this.notifyAll();;
return production;
}
}
class ProductionChannelTest{
public static void test(){
final ProductionChannel channel = new ProductionChannel(5);
AtomicInteger productionNo = new AtomicInteger();
IntStream.range(1,8).forEach(i->new Thread(()->{
while(true){
channel.offerProduction(new Production(productionNo.getAndIncrement()));
try{
TimeUnit.SECONDS.sleep(ThreadLocalRandom.current().nextInt(10));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start());
}
}
Worker-Thread设计模式的更多相关文章
- 多线程 Worker Thread 模式
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
- Worker Thread
http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...
- Simple Worker Thread Class
http://www.codeproject.com/Articles/36184/Simple-Worker-Thread-Class Introduction Many times we need ...
- 多线程系列之九:Worker Thread模式
一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...
- Exception thrown on Scheduler.Worker thread. Add `onError` handling
<html> <head></head> <body> java.lang.IllegalStateException: Exception throw ...
- Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server
MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...
- Do waiting or suspended tasks tie up a worker thread?
https://blogs.msdn.microsoft.com/askjay/2012/07/29/do-waiting-or-suspended-tasks-tie-up-a-worker-t ...
- Mongodb之failed to create service entry worker thread
Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...
- Worker Thread模式
工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来 5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带 public class C ...
- Worker Thread等到工作来,来了就工作
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
随机推荐
- asp.net下ueditor上传大容量视频报http请求错误的解决方法
故障现象: 当使用百度编辑器ueditor上传大容量视频或大容量图片的时候,编辑器报“http请求错误”的解决方法详解: 原因分析: 目前很多CMS整合了百度的ueditor编辑器,但是上传稍微大一点 ...
- 使用MinGW编译Boost,MSVC编译Boost的几种链接方式 good
1.下载Boost(http://www.boost.org) 我目前用的是1.61.0版本 2.将MinGW下的bin目录完整路径设置到系统环境变量Path中,保证cmd命令行能找到gcc,g++等 ...
- Ubuntu下使用Docker搭建MySQL步骤备忘
docker 安装和 pull MySQL镜像这里就不介绍了,很多介绍,建议去docker官方网站查看. 本文主要介绍MySQL container 运行起来之后的一些配置 在往下看之前,确保 doc ...
- 系统休眠消息PBT_APMSUSPEND
https://msdn.microsoft.com/en-us/library/windows/desktop/aa372721(v=vs.85).aspx https://msdn.microso ...
- EF 6.0 Code First 迁移MySql数据库
一.准备工作 使用NUGET安装Entity Framework 6,下载MySql Connector/Net 6.9.5 二.创建实体 我们在下面创建了两个类(博客和文章),并 ...
- Jstack线程状态BLOCKED/TIMED_WAITING/WAITING解释
一.线程5种状态 新建状态(New) 新创建了一个线程对象. 就绪状态(Runnable) 线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中,变得可运行,等待获 ...
- vue复选框获取值的补充
要通过vue的v-model获取选中复选框的值,可以用遍历对象的方式获取,代码如下: <!DOCTYPE html> <html xmlns="http://www.w3. ...
- Vim入门操作整理
根据小甲鱼的vim入门视频整理,供查阅 移动指令:上下左右 k j h l 翻页: ctrl + b ctrl + f 保存退出:ZZ 普通模式:vim fileName 首次进入的就是普通模式 从 ...
- 03 Javascript的数据类型
数据类型包括:基本数据类型和引用数据类型 基本数据类型指的是简单的数据段,引用数据类型指的是有多个值构成的对象. 当我们把变量赋值给一个变量时,解析器首先要确认的就是这个值是基本类型值还是引用类型值 ...
- surging 微服务引擎 2.0 会有多少惊喜?
surging 微服务引擎从2017年6月至今已经有两年的时间,这两年时间有多家公司使用surging 服务引擎,并且有公司搭建了CI/CD,并且使用了k8s 集群,这里我可以说下几家公司的服务搭建情 ...