工人线程Worker thread会逐个取回工作并进行处理,当所有工作全部完成后,工人线程会等待新的工作到来

5个工人线程从传送带取数据,3个传送工人线程将数据放入传送带

public class Channel {

    private final static int MAX_REQUEST = ;

    private final Request[] requestQueue;

    private int head;

    private int tail;

    private int count;

    private final WorkerThread[] workerPool;

    public Channel(int workers) {
this.requestQueue = new Request[MAX_REQUEST];
this.head = ;
this.tail = ;
this.count = ;
this.workerPool = new WorkerThread[workers];
this.init();
} private void init() {
for (int i = ; i < workerPool.length; i++) {
workerPool[i] = new WorkerThread("【工人:"+i+"】", this);
}
} /**
* push switch to start all of worker to work.
*/
public void startWorker() {
Arrays.asList(workerPool).forEach(WorkerThread::start);
} public synchronized void put(Request request) {
while (count >= requestQueue.length) {
try {
this.wait();
} catch (Exception e) {
}
} this.requestQueue[tail] = request;
this.tail = (tail + ) % requestQueue.length;
this.count++;
this.notifyAll();
} public synchronized Request take() {
while (count <= ) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} Request request = this.requestQueue[head];
this.head = (this.head + ) % this.requestQueue.length;
this.count--;
this.notifyAll();
return request;
}
}
public class Request {

    private final String name;

    private final int number;

    public Request(final String name, final int number) {
this.name = name;
this.number = number;
} public void execute() {
System.out.println(Thread.currentThread().getName() + " executed " + this);
} @Override
public String toString() {
return " Request=> No." + number + " Name." + name;
}
}
public class TransportThread extends Thread {
private final Channel channel; private static final Random random = new Random(System.currentTimeMillis()); public TransportThread(String name, Channel channel) {
super(name);
this.channel = channel;
} @Override
public void run() {
try {
for (int i = ; true; i++) {
Request request = new Request(getName(), i);
//向channel中放入request对象(谁放的,编号是多少)
this.channel.put(request);
Thread.sleep(random.nextInt(1_000));
}
} catch (Exception e) {
}
}
}
public class WorkerThread extends Thread {

    private final Channel channel;

    private static final Random random = new Random(System.currentTimeMillis());

    public WorkerThread(String name, Channel channel) {
super(name);
this.channel = channel;
} @Override
public void run() {
while (true) {
//取出request,执行里面的方法
channel.take().execute();
try {
Thread.sleep(random.nextInt(1_000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) { final Channel channel = new Channel();
channel.startWorker(); new TransportThread("Mark", channel).start();
new TransportThread("Jack", channel).start();
new TransportThread("Irish", channel).start();
}
}

Worker Thread模式的更多相关文章

  1. 多线程 Worker Thread 模式

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

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

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

  3. Worker Thread

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

  4. Simple Worker Thread Class

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

  5. 多线程程序设计学习(9)worker pattern模式

    Worker pattern[工作模式]一:Worker pattern的参与者--->Client(委托人线程)--->Channel(通道,里边有,存放请求的队列)--->Req ...

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

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

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

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

  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. 51Node1228序列求和 ——自然数幂和模板&&伯努利数

    伯努利数法 伯努利数原本就是处理等幂和的问题,可以推出 $$ \sum_{i=1}^{n}i^k={1\over{k+1}}\sum_{i=1}^{k+1}C_{k+1}^i*B_{k+1-i}*(n ...

  2. AtCoder Beginner Contest 132 解题报告

    前四题都好水.后面两道题好难. C Divide the Problems #include <cstdio> #include <algorithm> using names ...

  3. sql server 能按照自己规定的字段顺序展示

    工作中遇到,需要把sql 查询的按照指定的顺序显示 select plantname,cc_type,all_qty from VIEW_TEMP_DAY_CVT_CAP a where a.docd ...

  4. learning java 实例序列化

    对Person类实例进行序例化及反序例化: Person.java public class Person implements java.io.Serializable { private Stri ...

  5. Dart和JavaScript对比小结

    作为一名web前端来入门dart,新语言和我们熟悉的js有所差异,写dart的过程中容易受到原有思维的影响,这里把dart和js做一个对比总结,方便查找和熟悉. 变量声明 var 关键字 dart和j ...

  6. 洛谷 题解 P2721 【摄像头】

    这是我见过最水的蓝题 这不就是拓扑排序板子题吗 题目大意:松鼠砸烂摄像头不被抓住 摄像头一个可以监视到另一个可以看做有向边,用邻接链表储存就好了,我也不知道邻接矩阵到底能不能过保险起见还是用邻接链表. ...

  7. rust-vmm 学习(二)

    eventfd virtio中,guest和vhost通过evnetfd通知对方,见(Virtio ring and virtio-net). REF: Qemu-kvm的ioeventfd创建与触发 ...

  8. GIT 使用记录,新手->会用(mac 用户)

    (唔,mac 用户这个要求是因为集成在 terminal 中可直接使用) 1. github 中 创建 git 账户 2. github -> 在个人设置中 找到 ssh  and GPGkey ...

  9. elementUI 的el-dialog作为子组件,父组件如何控制其关闭的按钮

    这里有三点需要说明: 1. 使用:before-close="closeHandle" 将其 $emit() 出去 2. 取消按钮 也需要$emeit出去 3. 控制对话框显示隐藏 ...

  10. Python3教程—很经典可以快速上手

    原文地址:https://www.runoob.com/python3/python3-tutorial.html Python 3 教程 Python 的 3.0 版本,常被称为 Python 30 ...