📄 workerthread.htm
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>Worker Thread 模式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">Design Pattern: Worker Thread 模式</a></h1>
Worker Thread模式在Request的管理上像是 <a href="ProducerConsumer.htm">Producer Consumer 模式</a>,在Request的行为上像是 <a href="CommandPattern.htm">Command 模式</a>。<br>
<br>
Producer Consumer模式专注于Product的生产与消费,至于Product被消费时是作何处理,则不在它的讨论范围之中。 <br>
<div style="text-align: center;"><img style="width: 489px; height: 184px;" alt="WorkerThread" title="WorkerThread" src="images/workerThread-1.jpg"><br>
</div>
<br>
如果您的Product是一个Request,消费者取得Request之后,执行Request中指定的请求方法,也就是使用Command模式,并且您的Request缓冲区还管理了Consumer,就有Worker Thread模式的意思了。<br>
<div style="text-align: center;"><img style="width: 456px; height: 244px;" alt="WorkerThread" title="WorkerThread" src="images/workerThread-2.jpg"><br>
</div>
<br>
在Sequence Diagram上,可以看出Worker Thread同时展现了Producer Consumer模式与Command模式:<br>
<div style="text-align: center;"><img style="width: 491px; height: 400px;" alt="WorkThread" title="WorkThread" src="images/workerThread-3.jpg"></div>
利用Java实现的一个Channel类如下所示:<br>
<ul>
<li> Channel.java
</li>
</ul>
<pre>import java.util.LinkedList; <br><br>public class Channel { <br> private LinkedList requests; <br> private WorkerThread[] workerThreads; <br><br> public Channel(int threadNumber) { <br> requests = new LinkedList(); <br> workerThreads = new WorkerThread[threadNumber]; <br> for(int i = 0; i < workerThreads.size(); i++) { <br> workerThreads[i] = new WorkerThread(); <br> workerThreads[i].start(); <br> } <br> } <br><br> public synchronized void putRequest(Request request) { <br> while(requests.size() >= 2) { // 容量限制为 2 <br> try { <br> wait(); <br> } <br> catch(InterruptedException e) {} <br> } <br><br> requests.addLast(request); <br> notifyAll(); <br> } <br> <br> public synchronized Request getProduct() { <br> while(requests.size() <= 0) { <br> try { <br> wait(); <br> } <br> catch(InterruptedException e) {} <br> } <br><br> Request request = (Request) requests.removeFirst(); <br> notifyAll(); <br> <br> return request;<br> } <br>} <br></pre>
<br>
Request类与WorkerThread类之间采的Command模式:<br>
<ul>
<li> Request.java
</li>
</ul>
<pre>public class Request() { <br> // .... <br><br> public void execute() { <br> // do some work.... <br> } <br>} <br></pre>
<br>
<ul>
<li> WorkerThread.java
</li>
</ul>
<pre>public class WorkerThread extends Thread { <br> // ... <br><br> public void run() { <br> while(true) { <br> Request request = channel.getRequest(); <br> request.execute(); <br> } <br> } <br>} <br></pre>
<br>
就行为上,WorkerThread就是有请求来了就作,如果没有请求,则所有的WorkerThread就等待,直到有新的工作进来而通知它们,取得请
求的WorkerThread要作的工作,就直接定义在execute()中。 <br>
<br>
<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -