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等到工作来,来了就工作的更多相关文章

  1. 多线程 Worker Thread 模式

    Worker是“工人”的意思,worker thread pattern中,工人线程(worker thread)会一次抓一件工作来处理,当没有工作可做时,工人线程会停下来等待心得工作过来. Work ...

  2. 多线程系列之九:Worker Thread模式

    一,Worker Thread模式 也叫ThreadPool(线程池模式) 二,示例程序 情景:一个工作车间有多个工人处理请求,客户可以向车间添加请求.请求类:Request定义了请求的信息和处理该请 ...

  3. Scheduler & Task & Worker & Thread & Request & Session & Connection of SQL Server

    MSSQL一直以来被人们认为简单.好学,但等到大家掌握了入门操作,深入理解起来又觉得非常的“拧巴”,尤其是对用惯了Oracle的同学来说,究其根本原因,无非是MSSQL引入和暴露了太多的概念.细节和理 ...

  4. Worker Thread模式

    工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来 5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带 public class C ...

  5. Worker Thread

    http://www.codeproject.com/Articles/552/Using-Worker-Threads Introduction Worker threads are an eleg ...

  6. Simple Worker Thread Class

    http://www.codeproject.com/Articles/36184/Simple-Worker-Thread-Class Introduction Many times we need ...

  7. Exception thrown on Scheduler.Worker thread. Add `onError` handling

    <html> <head></head> <body> java.lang.IllegalStateException: Exception throw ...

  8. 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 ...

  9. Mongodb之failed to create service entry worker thread

    Mongodb "failed to create service entry worker thread" 错误. 系统:CentOS release 6.8 mongod.lo ...

随机推荐

  1. idea无法引用jar包中的class

    最近由eclipse换idea的过程中,出现了一个很奇妙的问题! 项目是maven+git+idea管理的,idea某次在使用的过程中,电脑死机重启后,发现无法引用jar包中的class.包括jdk中 ...

  2. Leetcode刷题——007.整数反转

    上代码: #include <cmath> class Solution { public: int reverse(int x) { ; long long tx=llabs(x); ) ...

  3. Guava Spiltter类

    Splitter 提供了各种方法来处理分割操作字符串,对象等. 类声明 以下是com.google.common.base.Splitter类的声明: @GwtCompatible(emulated= ...

  4. js小项目:显示与输入的内容相关的

    1,添加键盘抬起事件 2,获取文本框的内容,是否与数组中的内容匹配 3,创建元素 <!DOCTYPE html> <html lang="en"> < ...

  5. Excel函数——ANSI字符集与Code、Char、Asc函数

    小叙背景 Windows系统下,默认的字符集为ANSI,该字符编码方式在不同语言环境下采用不同的编码方案,在中文系统下ANSI编码是GBK.ANSI由ASCII扩展而来,ANSI下无论何种具体的编码方 ...

  6. Oracle之数据类型问题

    做项目涉及到Oracle数据库中数据类型:字符串型的问题 我不太清楚varchar(32)到底代表着什么? 通过搜索了解到:oracle中有三种常用的类型:varchar2(byte),varchar ...

  7. [学习笔记]最小割树(Gomory-Hu Tree)

    最小割树(\(\mathcal{Gomory-Hu Tree}\))简明指南 对于单源最短路径,我们有\(SPFA\)和\(Dijkstra\),对于多源最短路径,我们有\(Floyd\):对于两点间 ...

  8. word embedding 精要整理

    word embedding 具体含义:词的实数向量化表示,可以通过向量相似性度量语义相似性,相似性原理是上下文的一致性 Embedding在数学上表示一个maping, f: X -> Y, ...

  9. 使用osgearth2.9 rex引擎在Qt中黑屏的问题修复

    将osgUtil::RenderStage.cpp中的下列红色代码注释重新编译即可: if ( !colorAttached ) { setDrawBuffer( GL_NONE, true ); s ...

  10. Photon Server与Unity3D客户端的交互

    Photon Server与Unity3D的交互分为3篇博文实现 (1)Photon Server的服务器端配置 (2)Photon Server的Unity3D客户端配置 (3)Photon Ser ...