前面談了多線程應(yīng)用程序能極大地改善用戶相應(yīng)。例如對(duì)于一個(gè)Web應(yīng)用程序,每當(dāng)一個(gè)用戶請(qǐng)求服務(wù)器連接時(shí),服務(wù)器就可以啟動(dòng)一個(gè)新線程為用戶服務(wù)。
然而,創(chuàng)建和銷毀線程本身就有一定的開(kāi)銷,如果頻繁創(chuàng)建和銷毀線程,CPU和內(nèi)存開(kāi)銷就不可忽略,垃圾收集器還必須負(fù)擔(dān)更多的工作。因此,線程池就是為了避免頻繁創(chuàng)建和銷毀線程。
每當(dāng)服務(wù)器接受了一個(gè)新的請(qǐng)求后,服務(wù)器就從線程池中挑選一個(gè)等待的線程并執(zhí)行請(qǐng)求處理。處理完畢后,線程并不結(jié)束,而是轉(zhuǎn)為阻塞狀態(tài)再次被放入線程池中。這樣就避免了頻繁創(chuàng)建和銷毀線程。
Worker Pattern實(shí)現(xiàn)了類似線程池的功能。首先定義Task接口:
package com.crackj2ee.thread;
public interface Task {
void execute();
}
線程將負(fù)責(zé)執(zhí)行execute()方法。注意到任務(wù)是由子類通過(guò)實(shí)現(xiàn)execute()方法實(shí)現(xiàn)的,線程本身并不知道自己執(zhí)行的任務(wù)。它只負(fù)責(zé)運(yùn)行一個(gè)耗時(shí)的execute()方法。
具體任務(wù)由子類實(shí)現(xiàn),我們定義了一個(gè)CalculateTask和一個(gè)TimerTask:
// CalculateTask.java
package com.crackj2ee.thread;
public class CalculateTask implements Task {
private static int count = 0;
private int num = count;
public CalculateTask() {
count++;
}
public void execute() {
System.out.println("[CalculateTask " + num + "] start...");
try {
Thread.sleep(3000);
}
catch(InterruptedException ie) {}
System.out.println("[CalculateTask " + num + "] done.");
}
}
// TimerTask.java
package com.crackj2ee.thread;
public class TimerTask implements Task {
private static int count = 0;
private int num = count;
public TimerTask() {
count++;
}
public void execute() {
System.out.println("[TimerTask " + num + "] start...");
try {
Thread.sleep(2000);
}
catch(InterruptedException ie) {}
System.out.println("[TimerTask " + num + "] done.");
}
}
以上任務(wù)均簡(jiǎn)單的sleep若干秒。
TaskQueue實(shí)現(xiàn)了一個(gè)隊(duì)列,客戶端可以將請(qǐng)求放入隊(duì)列,服務(wù)器線程可以從隊(duì)列中取出任務(wù):
package com.crackj2ee.thread;
import java.util.*;
public class TaskQueue {
private List queue = new LinkedList();
public synchronized Task getTask() {
while(queue.size()==0) {
try {
this.wait();
}
catch(InterruptedException ie) {
return null;
}
}
return (Task)queue.remove(0);
}
public synchronized void putTask(Task task) {
queue.add(task);
this.notifyAll();
}
}
終于到了真正的WorkerThread,這是真正執(zhí)行任務(wù)的服務(wù)器線程:
package com.crackj2ee.thread;
public class WorkerThread extends Thread {
private static int count = 0;
private boolean busy = false;
private boolean stop = false;
private TaskQueue queue;
public WorkerThread(ThreadGroup group, TaskQueue queue) {
super(group, "worker-" + count);
count++;
this.queue = queue;
}
public void shutdown() {
stop = true;
this.interrupt();
try {
this.join();
}
catch(InterruptedException ie) {}
}
public boolean isIdle() {
return !busy;
}
public void run() {
System.out.println(getName() + " start.");
while(!stop) {
Task task = queue.getTask();
if(task!=null) {
busy = true;
task.execute();
busy = false;
}
}
System.out.println(getName() + " end.");
}
}
前面已經(jīng)講過(guò),queue.getTask()是一個(gè)阻塞方法,服務(wù)器線程可能在此wait()一段時(shí)間。此外,WorkerThread還有一個(gè)shutdown方法,用于安全結(jié)束線程。
最后是ThreadPool,負(fù)責(zé)管理所有的服務(wù)器線程,還可以動(dòng)態(tài)增加和減少線程數(shù):
package com.crackj2ee.thread;
import java.util.*;
public class ThreadPool extends ThreadGroup {
private List threads = new LinkedList();
private TaskQueue queue;
public ThreadPool(TaskQueue queue) {
super("Thread-Pool");
this.queue = queue;
}
public synchronized void addWorkerThread() {
Thread t = new WorkerThread(this, queue);
threads.add(t);
t.start();
}
public synchronized void removeWorkerThread() {
if(threads.size()>0) {
WorkerThread t = (WorkerThread)threads.remove(0);
t.shutdown();
}
}
public synchronized void currentStatus() {
System.out.println("-----------------------------------------------");
System.out.println("Thread count = " + threads.size());
Iterator it = threads.iterator();
然而,創(chuàng)建和銷毀線程本身就有一定的開(kāi)銷,如果頻繁創(chuàng)建和銷毀線程,CPU和內(nèi)存開(kāi)銷就不可忽略,垃圾收集器還必須負(fù)擔(dān)更多的工作。因此,線程池就是為了避免頻繁創(chuàng)建和銷毀線程。
每當(dāng)服務(wù)器接受了一個(gè)新的請(qǐng)求后,服務(wù)器就從線程池中挑選一個(gè)等待的線程并執(zhí)行請(qǐng)求處理。處理完畢后,線程并不結(jié)束,而是轉(zhuǎn)為阻塞狀態(tài)再次被放入線程池中。這樣就避免了頻繁創(chuàng)建和銷毀線程。
Worker Pattern實(shí)現(xiàn)了類似線程池的功能。首先定義Task接口:
package com.crackj2ee.thread;
public interface Task {
void execute();
}
線程將負(fù)責(zé)執(zhí)行execute()方法。注意到任務(wù)是由子類通過(guò)實(shí)現(xiàn)execute()方法實(shí)現(xiàn)的,線程本身并不知道自己執(zhí)行的任務(wù)。它只負(fù)責(zé)運(yùn)行一個(gè)耗時(shí)的execute()方法。
具體任務(wù)由子類實(shí)現(xiàn),我們定義了一個(gè)CalculateTask和一個(gè)TimerTask:
// CalculateTask.java
package com.crackj2ee.thread;
public class CalculateTask implements Task {
private static int count = 0;
private int num = count;
public CalculateTask() {
count++;
}
public void execute() {
System.out.println("[CalculateTask " + num + "] start...");
try {
Thread.sleep(3000);
}
catch(InterruptedException ie) {}
System.out.println("[CalculateTask " + num + "] done.");
}
}
// TimerTask.java
package com.crackj2ee.thread;
public class TimerTask implements Task {
private static int count = 0;
private int num = count;
public TimerTask() {
count++;
}
public void execute() {
System.out.println("[TimerTask " + num + "] start...");
try {
Thread.sleep(2000);
}
catch(InterruptedException ie) {}
System.out.println("[TimerTask " + num + "] done.");
}
}
以上任務(wù)均簡(jiǎn)單的sleep若干秒。
TaskQueue實(shí)現(xiàn)了一個(gè)隊(duì)列,客戶端可以將請(qǐng)求放入隊(duì)列,服務(wù)器線程可以從隊(duì)列中取出任務(wù):
package com.crackj2ee.thread;
import java.util.*;
public class TaskQueue {
private List queue = new LinkedList();
public synchronized Task getTask() {
while(queue.size()==0) {
try {
this.wait();
}
catch(InterruptedException ie) {
return null;
}
}
return (Task)queue.remove(0);
}
public synchronized void putTask(Task task) {
queue.add(task);
this.notifyAll();
}
}
終于到了真正的WorkerThread,這是真正執(zhí)行任務(wù)的服務(wù)器線程:
package com.crackj2ee.thread;
public class WorkerThread extends Thread {
private static int count = 0;
private boolean busy = false;
private boolean stop = false;
private TaskQueue queue;
public WorkerThread(ThreadGroup group, TaskQueue queue) {
super(group, "worker-" + count);
count++;
this.queue = queue;
}
public void shutdown() {
stop = true;
this.interrupt();
try {
this.join();
}
catch(InterruptedException ie) {}
}
public boolean isIdle() {
return !busy;
}
public void run() {
System.out.println(getName() + " start.");
while(!stop) {
Task task = queue.getTask();
if(task!=null) {
busy = true;
task.execute();
busy = false;
}
}
System.out.println(getName() + " end.");
}
}
前面已經(jīng)講過(guò),queue.getTask()是一個(gè)阻塞方法,服務(wù)器線程可能在此wait()一段時(shí)間。此外,WorkerThread還有一個(gè)shutdown方法,用于安全結(jié)束線程。
最后是ThreadPool,負(fù)責(zé)管理所有的服務(wù)器線程,還可以動(dòng)態(tài)增加和減少線程數(shù):
package com.crackj2ee.thread;
import java.util.*;
public class ThreadPool extends ThreadGroup {
private List threads = new LinkedList();
private TaskQueue queue;
public ThreadPool(TaskQueue queue) {
super("Thread-Pool");
this.queue = queue;
}
public synchronized void addWorkerThread() {
Thread t = new WorkerThread(this, queue);
threads.add(t);
t.start();
}
public synchronized void removeWorkerThread() {
if(threads.size()>0) {
WorkerThread t = (WorkerThread)threads.remove(0);
t.shutdown();
}
}
public synchronized void currentStatus() {
System.out.println("-----------------------------------------------");
System.out.println("Thread count = " + threads.size());
Iterator it = threads.iterator();