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. Linux 下tomcat+jdk+mysql安装

    JDK 1.用xftp把 jdk1.8.0_65上传到local下 给他赋予最高权限 1)切换到顶级目录    cd ~ 2)然后切换到上级目录 cd .. 3)切换到local路径下  cd /us ...

  2. localStorage使用总结(转载)

    localStorage使用总结  本文转载自:https://www.cnblogs.com/st-leslie/p/5617130.html(点击标题可跳转至原文) 一.什么是localStora ...

  3. Java控制台

    Console类的目的是使Java程序和控制台之间的交互更容易.Console类是java.io包中的一个实用程序类,用于访问系统控制台.控制台不能保证在所有机器上的Java程序中可访问. 例如,如果 ...

  4. 入门 Webpack,看这篇就够

    写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...

  5. 判断IE版本与各浏览器的语句

    ---恢复内容开始--- 一.IE下判断IE版本的语句 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见    <!--[if lt ...

  6. js面向对象(一)---基本的概念、属性、方法

    一.什么是面向对象编程 1.用对象的思想去写代码,就是面向对象编程 2.我们一直在使用对象,如数组Array    时间Date //我们把系统自带的对象,叫做系统对象 var arr = new A ...

  7. jp@gc - Stepping Thread Group (deprecated)

      并发6个用户,线程之前不等待,每隔3秒增加1个用户,间隔时间是2秒,然后并发数增加完成之后,运行60秒,运行完成后,每1秒钟停止2个用户

  8. 22-4-isarry

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 36. 解决线程问题方式一(同步代码块synchronized)

    解决线程问题: 方式一:同步代码块(synchronized) 语法: synchronized ("锁对象") {             //需要锁定的代码       }   ...

  10. vscode编程nodejs初始安装

    nodejs官网 http://nodejs.cn/ 1.安装nodejs,记得安装时勾选配置路径 在cmd中输入node,进去node环境即为安装成功. 2.安装vscode,并安装插件node e ...