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 ...
随机推荐
- monkeyrunner 进行多设备UI测试
monkeyrunner进行多设备UI测试 首先你要连接好多个手机设置好已经连接好的手机的ip列表ipp = ['192xxx','192xxx']杀掉之前所有appium进程subprocess. ...
- mysql 审核
https://javinjunfeng.top/technicalstack/database/43
- 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_自定义控件--主要用于画面拖拽效果)
如题,需求:在某个图片上用户可以手动指定位置. 如下: 中心思想:仿照Visual Studio工具中的控件的做法 如何仿照呢? 1.自定义的控件类继承System.Windows.Forms.Con ...
- Jeecg 切换默认首页方法
类名: org.jeecgframework.core.enums.SysThemesEnum
- 2、什么是session?
session 什么是Session?Session什么时候产生? Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这 ...
- package com.nps.base.xue.xd.groovyEngine import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.nps.common.service.NpsApplicationContextHolder import com.nps.data_api.service
原因: Switch this to "false" to let the transaction originator make the rollback decision. I ...
- HDU 6656 Kejin Player (期望DP 逆元)
2019 杭电多校 7 1011 题目链接:HDU 6656 比赛链接:2019 Multi-University Training Contest 7 Problem Description Cub ...
- Java DOM解析器
文档对象模型是万维网联盟(W3C)的官方推荐.它定义了一个接口,使程序能够访问和更新样式,结构和XML文档的内容.支持DOM实现该接口的XML解析器. 何时使用? 在以下几种情况时,应该使用DOM解析 ...
- 自学Oracle心得
基本术语: global name 全局数据库名 service name 服务名 SID 实例名 常用命令: 1. s ...
- echart 柱状图背景色设置
方法一: series: [ { name: '1', type: 'bar', itemStyle: {//柱图背景色 color: '#111' }, data: list }, { name: ...