多程线设计模式 -- woker thread pattern

工作区(核心)
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 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 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 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 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();
}
}


1.启动线程是繁重的操作
如果可以把自己的工作交给别人做,自己的可以去做其他事情.线程也一亲.如果可以把工作交给其他线程,自己就可以继续进到下一个工作.这是Thread-Per-Message Pattern 的主题.

2.控制承载量
Worker Thread 还有一个主题.就是承载量的控制.
Worker 参与者的数量可以根据设置大小.

3. invocation(启动方法)与execution(执行方法)分离
提高响应性
控制实行顺序
可取消和可重复执行
分散处理的第一步
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值