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 ...
随机推荐
- socket模拟通信
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java ...
- [资料]C#操作SQL Server数据库
1.概述 ado.net提供了丰富的数据库操作,这些操作可以分为三个步骤: 第一,使用SqlConnection对象连接数据库: 第二,建立SqlCommand对象,负责SQL语句的执行和存储过程的调 ...
- python_way ,day22 tonardo,jsonp
python_way day22 1.tonardo 2.cookie 3.api认证 一.tonardo: a.tonardo 初识 #!/usr/bin/env python3# Created ...
- C# 设计模式 (一)
学习来自<大话设计模式>有兴趣可以研究一下 一.简单工厂模式 原理 子类对象可以赋值给父类对象.同一个父类对象的子类,通过switch语句new出来(当然用if也可以但那样判断就多了起来) ...
- 二维RMQ hdu 2888
题目:点这里 题意:给出一个n*m的矩阵,然后又Q个询问:每个询问有x1,y1,x2,y2,x1,y1为子矩阵的左上角坐标,x2,y2为右上角的坐标.求此子矩阵中元素最大值,判断最大值是否在子矩阵四个 ...
- js 万能判断
console.log(Object.prototype.toString.call(123)) //[object Number] console.log(Object.prototype.toSt ...
- Vue+element ui table 导出到excel
需求: Vue+element UI table下的根据搜索条件导出当前所有数据 参考: https://blog.csdn.net/u010427666/article/details/792081 ...
- 关于apache 重定向设定
本人在研究关于apache重定向的资料,在网上找了很多,但是就本人来说,方便理解的,找到了这么一个,记录了下来,原帖地址:http://www.exehack.net/8.html 关于apache配 ...
- fork执行一个进程
https://coolr321.github.io/2018/10/30/%E4%B8%80%E4%B8%AAfork-%E8%B0%83%E7%94%A8%E7%9A%84%E4%BE%8B%E5 ...
- python编写微信公众号首图思路详解
前言 之前一直在美图秀秀调整自己的微信公众号首图,效果也不尽如人意,老是调来调去,最后发出来的图片被裁剪了一大部分,丢失部分关键信息,十分恼火,于是想着用python写一个程序,把微信公众号首图的模式 ...