Worker Thread模式
工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来
5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带
public class Channel {
private final static int MAX_REQUEST = ;
private final Request[] requestQueue;
private int head;
private int tail;
private int count;
private final WorkerThread[] workerPool;
public Channel(int workers) {
this.requestQueue = new Request[MAX_REQUEST];
this.head = ;
this.tail = ;
this.count = ;
this.workerPool = new WorkerThread[workers];
this.init();
}
private void init() {
for (int i = ; i < workerPool.length; i++) {
workerPool[i] = new WorkerThread("【工人:"+i+"】", this);
}
}
/**
* push switch to start all of worker to work.
*/
public void startWorker() {
Arrays.asList(workerPool).forEach(WorkerThread::start);
}
public synchronized void put(Request request) {
while (count >= requestQueue.length) {
try {
this.wait();
} catch (Exception e) {
}
}
this.requestQueue[tail] = request;
this.tail = (tail + ) % requestQueue.length;
this.count++;
this.notifyAll();
}
public synchronized Request take() {
while (count <= ) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Request request = this.requestQueue[head];
this.head = (this.head + ) % this.requestQueue.length;
this.count--;
this.notifyAll();
return request;
}
}
public class Request {
private final String name;
private final int number;
public Request(final String name, final int number) {
this.name = name;
this.number = number;
}
public void execute() {
System.out.println(Thread.currentThread().getName() + " executed " + this);
}
@Override
public String toString() {
return " Request=> No." + number + " Name." + name;
}
}
public class TransportThread extends Thread {
private final Channel channel;
private static final Random random = new Random(System.currentTimeMillis());
public TransportThread(String name, Channel channel) {
super(name);
this.channel = channel;
}
@Override
public void run() {
try {
for (int i = ; true; i++) {
Request request = new Request(getName(), i);
//向channel中放入request对象(谁放的,编号是多少)
this.channel.put(request);
Thread.sleep(random.nextInt(1_000));
}
} catch (Exception e) {
}
}
}
public class WorkerThread extends Thread {
private final Channel channel;
private static final Random random = new Random(System.currentTimeMillis());
public WorkerThread(String name, Channel channel) {
super(name);
this.channel = channel;
}
@Override
public void run() {
while (true) {
//取出request,执行里面的方法
channel.take().execute();
try {
Thread.sleep(random.nextInt(1_000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
final Channel channel = new Channel();
channel.startWorker();
new TransportThread("Mark", channel).start();
new TransportThread("Jack", channel).start();
new TransportThread("Irish", channel).start();
}
}
Worker Thread模式的更多相关文章
- 多线程 Worker Thread 模式
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
- 多线程系列之九:Worker Thread模式
一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...
- 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 ...
- 多线程程序设计学习(9)worker pattern模式
Worker pattern[工作模式]一:Worker pattern的参与者--->Client(委托人线程)--->Channel(通道,里边有,存放请求的队列)--->Req ...
- 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 ...
随机推荐
- Best practices for a new Go developer
https://blog.rubylearning.com/best-practices-for-a-new-go-developer-8660384302fc This year I had the ...
- js遍历localStorage的键值对
//遍历本地存储localStorage for (var i = 0; i < localStorage.length; i++) { var key = localStorage.key(i ...
- Python面向对象 -- slots, @property、多重继承MixIn、定制类(str, iter, getitem, getattr, call, callable函数,可调用对象)、元类(type, metaclass)
面向对象设计中最基础的3个概念:数据封装.继承和多态 动态给class增加功能 正常情况下,当定义了一个class,然后创建了一个class的实例后,可以在程序运行的过程中给该实例绑定任何属性和方法, ...
- lyft amundsen简单试用
昨天有说过amundsen 官方为我们提供了dockerc-compose 运行的参考配置,以下是一个来自官方的 quick start clone amundsen 代码 amundsen 使用了g ...
- 用Python操作MySQL(pymysql)
用python来操作MySQL,首先需要安装PyMySQL库(pip install pymysql). 连接MySQL: import pymysql connect=pymysql.connect ...
- CDH 版本 6.0.1 升级到 6.2.0 当前最新版本(CentOS 7.x)
前文「CDH CM版本 6.0.1 升级到 CM 6.2.0 当前最新版本(CentOS 7.x)」 承接上文,当我们完成 CM 6.2.0 的升级之后,我们已经相当于完成了80% minor 的升级 ...
- GoCN每日新闻(2019-10-15)
GoCN每日新闻(2019-10-15) GoCN每日新闻(2019-10-15) 1. Go Module 存在的意义与解决的问题 https://www.ardanlabs.com/blog/20 ...
- nRF51822 硬件复位引脚
nRF51822 有一个硬件复位引脚和Debug 口SWDIO是共用的,名字叫做nReset. 实现硬件复位是怎样子的: 1.这个引脚引出来, 2.给这个引脚低电平, 3.从低电平拉到高电平,即复位. ...
- /usr/bin/xauth: file /home/user/.Xauthority does not exist
错误信息如下: /usr/bin/xauth: file /home/user/.Xauthority does not exist 错误原因:是因为添加用户时没有授权对应的目录,仅仅执行了usera ...
- edusoho上传视频弹出abort之解决方案
错误描述:edusoho上传如avi.mp4等容量大的图片(如100m以上或500m等)弹出abort提示框 原因:是因为web服务器apache默认上传文件有限制导致的 解决办法如下: (1)首先修 ...