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 ...
随机推荐
- 全局异常捕获处理-@ControllerAdvice+@HandleException
涂涂影院管理系统这个demo中有个异常管理的标签,用于捕获 涂涂影院APP用户异常信息 ,有小伙伴好奇,排除APP,后台端的是如何处理全局异常的,故项目中的实际应用已记之. 关于目前的异常处理 在使用 ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- Codeforces Round #493 (Div. 2) 【A,B,C】
简单思维题 #include<bits/stdc++.h> using namespace std; #define int long long #define inf 0x3f3f3f3 ...
- 洛谷 题解 P1828 【香甜的黄油 Sweet Butter】
潇洒の开始 第一步:食用头文件和定义变量, 变量干什么用的说的很清楚 #include<iostream> #include<cstdio> #include<cstri ...
- manjaro AwesomeWM 上使用双显示器
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/dual_monitor_manjaro_awesome 安装ma ...
- 第12组 Alpha冲刺(6/6)
Header 队名:To Be Done 组长博客 作业博客 团队项目进行情况 燃尽图(组内共享) 展示Git当日代码/文档签入记录(组内共享) 注: 由于GitHub的免费范围内对多人开发存在较多限 ...
- Linux 磁盘的分区
如果我们想在系统中新增一块硬盘,需要做什么呢? 1. 对磁盘进行分区,新建可用分区 2. 对该分区进行格式化,以创建系统可用的文件系统 3. 若想要仔细一点,可以对刚才新建好的文件系统进行检验 4. ...
- EXCEL复制可见单元格
Excel筛选后,复制筛选后的单元格 1, 首先还是复制这一部分内容. 2, CTRL+G打开 "定位"窗口. 3, 在 "定位"窗口中选择"定位条件 ...
- ubuntu之路——day14 只用python的numpy在底层实现多层神经网络
首先感谢这位博主整理的Andrew Ng的deeplearning.ai的相关作业:https://blog.csdn.net/u013733326/article/details/79827273 ...
- zipkin exec下载地址
zipkin exec下载地址 https://repo1.maven.org/maven2/io/zipkin/java/zipkin-server/1.20.1/zipkin-server-1.2 ...