public class Request {
private final String name; // 委托者
private final int number; // 请求编号
private static final Random random = new Random();
public Request(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(1000));
} catch (InterruptedException e) {
}
}
public String toString() {
return "[ Request from " + name + " No." + number + " ]";
}
}
public class ClientThread extends Thread {
private final Channel channel;
private static final Random random = new Random();
public ClientThread(String name, Channel channel) {
super(name);
this.channel = channel;
}
public void run() {
try {
for (int i = 0; true; i++) {
Request request = new Request(getName(), i);
channel.putRequest(request);
Thread.sleep(random.nextInt(1000));
}
} catch (InterruptedException e) {
}
}
}
public class WorkerThread extends Thread {
private final Channel channel;
public WorkerThread(String name, Channel channel) {
super(name);
this.channel = channel;
}
public void run() {
while (true) {
Request request = channel.takeRequest();
request.execute();
}
}
}
public class Channel {
private static final int MAX_REQUEST = 100;
private final Request[] requestQueue;
private int tail; // 下一个putRequest的地方
private int head; // 下一个takeRequest的地方
private int count; // Request的数量
private final WorkerThread[] threadPool;
public Channel(int threads) {
this.requestQueue = new Request[MAX_REQUEST];
this.head = 0;
this.tail = 0;
this.count = 0;
threadPool = new WorkerThread[threads];
for (int i = 0; i < threadPool.length; i++) {
threadPool[i] = new WorkerThread("Worker-" + i, this);
}
}
public void startWorkers() {
for (int i = 0; i < threadPool.length; i++) {
threadPool[i].start();
}
}
public synchronized void putRequest(Request request) {
while (count >= requestQueue.length) {
try {
wait();
} catch (InterruptedException e) {
}
}
requestQueue[tail] = request;
tail = (tail + 1) % requestQueue.length;
count++;
notifyAll();
}
public synchronized Request takeRequest() {
while (count <= 0) {
try {
wait();
} catch (InterruptedException e) {
}
}
Request request = requestQueue[head];
head = (head + 1) % requestQueue.length;
count--;
notifyAll();
return request;
}
}
public class Main {
public static void main(String[] args) {
Channel channel = new Channel(5); // 工人线程的數量
channel.startWorkers();
new ClientThread("Alice", channel).start();
new ClientThread("Bobby", channel).start();
new ClientThread("Chris", channel).start();
}
}
分享到:
相关推荐
书中包含Java线程的介绍导读、12个重要的线程设计模式和全书总结以及丰富的附录内容。第一章相关线程设计模式的介绍,都举一反三使读者学习更有效。最后附上练习问题,让读者可以温故而知新,能快速地吸收书中的...
java多线程设计模式 线程的创建和重起 线程的同步 wait/notify/sleep机制 Worker Pattern
目录: 漫谈UML Introduction 1 Java语言的线程 Introduction 2 多线程...总结 多线程程序设计的模式语言 附录A 练习问题的解答 附录B Java的内存模型 附录C Java线程的优先级 附录D 线程相关的主要API 附录E 参考文献
线程示例WorkerThread_demo,线程示例WorkerThread_demo, 线程示例WorkerThread_demo,
Java多线程模式详解 目录: 一、漫谈UML Java语言的线程 多线程的评量标准 二、 1、Single Threaded Execution ———— 能通过这座桥的,只有一个人 2、Immutable ———— 想破坏它也没办法 3、Guarded ...
36种最新设计模式整理 Design Pattern: Simple Factory 模式 ... Worker Thread 模式 Thread-Per-Message 模式 Future 模式 Read-Write-Lock 模式 Two-phase Termination 模式 Thread-Specific Storage 模式
WorkerThread线程实例,C#建立WorkerThread "工作者线程"的例子,代码中已定义工作者线程中调用的公用委托,建立事件标识,用于停止线程,建立委托实例,用于调用接口函数,自定义了一个工作者线程处理函数,若停止...
用于《vue3中使用Web Worker多线程》这篇文章的项目Demo下载 文章地址:https://blog.csdn.net/weixin_42063951/article/details/125300644
用于《vue3中使用Web Worker多线程》这篇文章的项目Demo下载 文章地址:https://blog.csdn.net/weixin_42063951/article/details/125300644
AndroidChannel is helper library for inter thread communication between main thread and worker thread. AndroidChannel uses HandlerThread for inter thread communication. Setup Gradle dependencies { .....
flash builder在版本4.7中正式提供多线程的类,worker,此例子很不错。
Master-Worker是一个很好用的设计模式, 它可以把一个大工作分成很多小工作去执行, 等所有小工作都回来以后, 将所有的小工作结果做一个统整, 得到最终结果, 就很像专题小组有五个人, 把系统分成四等分, 每个人负责一...
圆周率计算,多线程,基于大数框架decimal.js和webworker 圆周率计算-可设置圆周率位数-可选择线程个数-多线程大数框架webworker输出useragent大数框架
C#backgroundworker多线程实用例子
多线程类worker的使用,接上一个资源,这是第2部分...
多个 Poster 或 Worker 客户端可以同时运行,以在 Kinesis 流上生成多线程负载。 入门 要使此示例与 Python 2.7+ 一起使用,请首先使用以下命令安装 boto 2.23+: $ pip install boto 并按照的说明设置您的凭据以...
SimpleFork提供一套类似于JAVA多线程的进程控制接口,提供回收、同步、互斥、通信等方案,开发者可以关注业务问题,不需要过多考虑进程控制。 引入 composer require jenner/simple_fork require path/to/...
其中,最重要的一个便是对多线程的支持。在HTML5中提出了工作线程(WebWorker)的概念,并且规范出WebWorker的三大主要特征:能够长时间运行(响应),理想的启动性能以及理想的内存消耗。WebWorker允许开发人员编写...
多线程编程中要注意协调好各个线程的优先级。一般来说,控制线程的优先级要高于Worker线程。这样做,可以保证Client(最终用户或者其他模块)尽快得到响应。当控制线程是与最终用户交互的界面线程时更应如此,如果...
不过Web Worker实际上受到很多限制,因为它无法真正意义上共享内存数据,只能通过消息来做状态通知,所以甚至不能称之为真正意义上的“多线程”。 Web Worker的接口使用起来很不方便,它基本上自带一个sandbox,在...