【图解Java多线程设计模式】Worker Thread模式

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

示例

ClientThread类的线程会向Channel类发送工作请求(委托)。
Channel类的实例雇用了五个工人线程(WorkerThread)进行工作。所有工人线程都在等待工作请求的到来。
工作请求到来后,工人线程会从Channel那里获取一项工作请求并开始工作。工作完成后,工人线程会回到Channel那里等待下一项工作请求。

类图

Fvr1mQ.png

时序图

Fvr2p6.png

Main.java

1
2
3
4
5
6
7
8
9
10
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();
}
}

ClientThread.java

  • 创建Request的实例
  • 将该实例传递给Channel类的putRequest方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Random;

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; ; i++) {
Request request = new Request(getName(), i);
channel.putRequest(request);
Thread.sleep(random.nextInt(1000));
}
} catch (InterruptedException e) {
}
}
}

Request.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Random;

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 + " ]";
}
}

Channel.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Channel {
private static final int MAX_REQUEST = 100;
private final Request[] requestQueue;
private final WorkerThread[] threadPool;
private int head;
private int tail;
private int count;

public Channel(int threads) {
this.requestQueue = new Request[MAX_REQUEST];
this.threadPool = new WorkerThread[threads];
this.head = 0;
this.tail = 0;
this.count = 0;

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;
}
}

WorkerThread.java

  • 调用takeRequest方法从Channel的实例中获取一个Request的实例
  • 调用Request的实例的execute方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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();
}
}
}

运行结果

1
2
3
4
5
6
7
8
9
Worker-3 executes [ Request from Bobby No.0 ]
Worker-0 executes [ Request from Chris No.0 ]
Worker-4 executes [ Request from Alice No.0 ]
Worker-0 executes [ Request from Chris No.1 ]
Worker-4 executes [ Request from Chris No.2 ]
Worker-3 executes [ Request from Bobby No.1 ]
Worker-1 executes [ Request from Alice No.1 ]
Worker-1 executes [ Request from Bobby No.2 ]
Worker-4 executes [ Request from Chris No.3 ]

登场角色

Client(委托者)

Client角色创建表示工作请求的Request角色并将其传递给Channel角色。在示例程序中,由ClientThread类扮演此角色。

Channel(通信线路)

Channel角色接收来自于Client角色的Request角色,并将其传递给Worker角色。在示例程序中,由Channel类扮演此角色。

Worker(工人)

Worker角色从Channel角色中获取Request角色,并进行工作。当一项工作完成后,它会继续去获取另外的Request角色。在示例程序中,由WorkerThread类扮演此角色。

Request(请求)

Request角色是表示工作的角色。Request角色中保存了进行工作所必需的信息。在示例程序中,由Request类扮演此角色。

类图

kVgqQP.png

Timethreads图

kV2Zo4.png