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 ...
随机推荐
- 常见的transformation算子
RDD:RDD分区数,若从HDFS创建RDD,RDD的分区就是和文件块一一对应,若是集合并行化形式创建,RDD分区数可以指定,一般默认值是CPU的核数. task:task数量就是和分区数量对应. 一 ...
- go 学习 (三):函数 & 指针 & 结构体
一.函数 函数声明 // 声明语法 func funcName (paramName paramType, ...) (returnType, returnType...) { // do somet ...
- VSFTPD匿名用户上传文件
1.安装vsftpd yum -y install vsftpd yum -y install ftp 客户端 2.编写配置文件 vim /etc/vsftpd/vsftpd.conf anonymo ...
- vue基本使用及脚手架使用
一.基本使用: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- learning java FileOutputStream
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- Vue之路由
1. SPA是什么 单页Web应用(single page application,SPA),就是只有一个Web页面的应用, 是加载单个HTML页面,并在用户与应用程序交互时动态更新该页面的Web应用 ...
- 使用merge-graphql-schemas 进行graphql schema 以及resovler 合并
merge-graphql-schemas 是一个方便的工具,可以进行schema 以及resovler 的合并处理 一个schema 合并参考demo schema 定义 // ./graphql/ ...
- zabbix显示 get value from agent failed:cannot connetct to xxxx:10050:[4] interrupted system call
在阿里云上部署的两台云主机,从server上 agent.ping不通agent10050端口,在agent上使用firewalld-cmd 添加了10050端口还不行,关闭了防火墙和selinux也 ...
- Codevs 3322 时空跳跃者的困境(组合数 二项式定理)
3322 时空跳跃者的困境 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 背景:收集完能量的圣殿战士suntian开始了他的追 ...
- P2624 [HNOI2008]明明的烦恼
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #inclu ...