一、定義
Work Thread模式和Thread-Per-Message模式類似棋凳,Thread-Per-Message每次都創(chuàng)建一個新的線程處理請求朝巫,而Work Thread模式預(yù)先會創(chuàng)建一個線程池(Thread Pool)妥箕,每次從線程池中取出線程處理請求滥酥。
二、模式案例
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 + " ]";
}
}
Client線程類:
Client線程類用來送出請求:
- 創(chuàng)建Request實(shí)例
- 將這個實(shí)例傳送給Channel類的putRequest方法
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) {
}
}
}
Worker線程類:
WorkerThread類表示工人線程矾踱,工人線程可以執(zhí)行以下動作:
- 從Channel實(shí)例取出Request實(shí)例
- 調(diào)用Request實(shí)例的execute方法
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();
}
}
}
Channel類:
//Channel類可用來接受恨狈、傳送工作請求疏哗,并保存工人線程呛讲。
public class Channel {
private static final int MAX_REQUEST = 100; // 最大請求數(shù)
private final Request[] requestQueue; // 請求隊列
private int tail;
private int head;
private int count;
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;
}
}
執(zhí)行:
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();
}
}
三、模式講解
Work Thread模式的角色如下:
- Client(委托人)參與者
Client參與者會創(chuàng)建請求(Request)返奉,然后傳送給Channel參與者贝搁。 - Channel(通道)參與者
Channel參與者保存Request請求隊列,同時會預(yù)創(chuàng)建Worker線程芽偏。 - Worker(工人)參與者
Worker參與者會從Channel獲取Request雷逆。 - Request(請求)參與者
Worker參與者會從Channel獲取Request。
注:啟動線程是一項繁重的工作污尉,Worker Thread模式預(yù)先創(chuàng)建一批線程膀哲,可以重復(fù)使用線程,達(dá)到資源再利用被碗、提升性能的目的某宪。