Worker Thread等到工作来,来了就工作
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来。
Worker Thread也叫做background thread,另外,也有人把视点放在管理工人线程的地方,称之为Thread Pool。
public class WorkerThreadTest {
/**
* @param args
*/
public static void main(String[] args) {
Channel channel = new Channel();
channel.startWorkers();
new WorkerClientThread("Alice", channel).start();
new WorkerClientThread("Bobby", channel).start();
new WorkerClientThread("Chris", channel).start();
}
}
class WorkerClientThread extends Thread{
private final Channel channel;
private static final Random random = new Random();
public WorkerClientThread(String name,Channel channel){
super(name);
this.channel=channel;
}
@Override
public void run() {
try{
for(int i=;true; i++){
WorkerRequest request = new WorkerRequest(getName(),i);
channel.put(request);
Thread.sleep(random.nextInt());
}
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
class WorkerRequest{
private final String name;
private final int number;
private static final Random random = new Random();
public WorkerRequest(String name,int number){
this.name=name;
this.number=number;
}
public void execute(){
System.out.println(Thread.currentThread().getName() + " executes " + this);
try {
Thread.sleep(random.nextInt());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "[Request from " + name + " No." + number + "]";
}
}
class Channel{
private static final int MAX_REQUEST=;
private final WorkerRequest[] requestQueue;
private int tail;
private int head;
private int count;
private final WorkerThread[] threadPool;
public Channel(int threads){
this.requestQueue=new WorkerRequest[MAX_REQUEST];
this.head = ;
this.tail=;
this.count=;
threadPool=new WorkerThread[threads];
for(int i=;i < threadPool.length;i++){
threadPool[i] = new WorkerThread("Worker-" + i, this);
}
}
public void startWorkers(){
for(int i=;i<threadPool.length;i++){
threadPool[i].start();
}
}
public synchronized void put(WorkerRequest request){
while(count>=requestQueue.length){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
requestQueue[tail]=request;
tail=(tail+)%requestQueue.length;
count++;
notify();
}
public synchronized WorkerRequest take(){
while(count<=){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WorkerRequest request = requestQueue[head];
head=(head+)%requestQueue.length;
count--;
notify();
return request;
}
}
class WorkerThread extends Thread{
private final Channel channel;
public WorkerThread(String name, Channel channel){
super(name);
this.channel=channel;
}
@Override
public void run() {
while(true){
WorkerRequest request = channel.take();
request.execute();
}
}
}
Worker Thread等到工作来,来了就工作的更多相关文章
- 多线程 Worker Thread 模式
Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...
- 多线程系列之九:Worker Thread模式
一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...
- Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server
MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...
- Worker Thread模式
工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来 5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带 public class C ...
- 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 ...
- Exception thrown on Scheduler.Worker thread. Add `onError` handling
<html> <head></head> <body> java.lang.IllegalStateException: Exception throw ...
- 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 ...
随机推荐
- flask 的orm
https://www.cnblogs.com/chichung/p/9794702.html
- could not stop cortex-m device
检查一下STM32复位管脚是不是0V,如果是0V的话并且你有上拉电阻,那么就断电后检查一下STM32的VCC和GND是否短路,我的就是两个贴片电容击穿造成的短路从而使RST无法拉高.
- 正则化:L0 vs L1 vs L2
原文地址:https://www.jianshu.com/p/e5c9a9fc84d4 为什么正则化可以缓解过拟合? 过拟合时,拟合函数的系数往往非常大.过大的权重会导致模型过多地学习到某些数据的个性 ...
- [Flink原理介绍第四篇】:Flink的Checkpoint和Savepoint介绍
原文:https://blog.csdn.net/hxcaifly/article/details/84673292 https://blog.csdn.net/zero__007/article/d ...
- 错误 1 error C4996: 'getcwd': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getcwd. See online help for details.
解决办法: 属性>C/C++>预处理定义>编辑>添加_CRT_NONSTDC_NO_DEPRECATE>应用
- Spring MVC源码分析(二):SpringMVC的DispatcherServlet的设计与实现
概述 DispatcherServlet是SpringMVC的一个前端控制器,是MVC架构中的C,即controller的实现,用于拦截这个web应用的所有请求,具体为在web.xml中配置这个s ...
- C语言指针和数组
#include <stdio.h> int main() { /********************************************* * * 指针和数组: * 定义 ...
- the blank final field factors may not have been initialized
Q1: why we should initialize final field before completion of new instance? final means no changeabl ...
- 解决Google Chrome浏览器字体模糊的问题
之前使用Google的Chrome浏览器一直觉得有时候,其显示的字体比较模糊,不管是Windows XP还是Windows 7都会出现要么显示的网页字体模糊,要么是Chrome浏览器本身显示的菜单模糊 ...
- wireshark 分析 TCP 请求(转)
转自:http://supben.iteye.com/blog/2329780 先看一段代码 程序片段是一个RPC调用 ,根据简历id获取简历实体.本地IP 10.252.156.132, 远程ip ...