頻繁創建/銷毀線程開銷大(內核對象、棧內存)。線程池復用線程、控制併發數、管理任務隊列,是高併發系統必備組件。
LinkedList + synchronized 實現生產消費模型。PooledThread 循環 takeTask() → execute()。public interface Task {
void perform() throws Exception;
}
public class MyTask implements Task {
private final int taskID;
public MyTask(int id) { this.taskID = id; }
@Override
public void perform() throws Exception {
System.out.println("MyTask " + taskID + ":start");
Thread.sleep(1000);
System.out.println("MyTask " + taskID + ":end");
}
}
public class MyThreadPool extends ThreadGroup {
private volatile boolean alive = true;
private final LinkedList<Task> taskQueue = new LinkedList<>();
private int threadID = 0;
private static int poolID = 0;
public MyThreadPool(int numThreads) {
super("ThreadPool-" + (poolID++));
setDaemon(true); // 所有線程結束自動銷毀線程組
for (int i = 0; i < numThreads; i++) new PooledThread().start();
}
// 提交任務
public synchronized void performTask(Task task) {
if (!alive) throw new IllegalStateException("線程池已關閉");
if (task != null) {
taskQueue.addLast(task);
notify(); // 喚醒一個等待線程
}
}
// 獲取任務(阻塞)
protected synchronized Task getTask() throws InterruptedException {
while (taskQueue.isEmpty()) {
if (!alive) return null; // 池關閉且無任務
wait();
}
return taskQueue.removeFirst();
}
// 立即關閉
public synchronized void close() {
if (alive) {
alive = false;
taskQueue.clear();
interrupt(); // ThreadGroup.interrupt() → 所有線程中斷
}
}
// 優雅關閉
public void join() {
synchronized (this) {
alive = false;
notifyAll();
}
Thread[] threads = new Thread[activeCount()];
int count = enumerate(threads);
for (int i = 0; i < count; i++) {
try { threads[i].join(); } catch (InterruptedException ignored) {}
}
}
// 工作線程
private class PooledThread extends Thread {
PooledThread() { super(MyThreadPool.this, "PooledThread-" + (threadID++)); }
@Override public void run() {
while (!isInterrupted()) {
Task task;
try { task = getTask(); } catch (InterruptedException e) { return; }
if (task == null) return; // 池關閉
try { task.perform(); }
catch (Exception e) { uncaughtException(this, e); }
}
}
}
}
public class PoolTest {
public static void main(String[] args) {
MyThreadPool pool = new MyThreadPool(3); // 3 個工作線程
for (int i = 0; i < 10; i++) pool.performTask(new MyTask(i));
pool.join(); // 等待所有任務完成
}
}
MyTask 0:start MyTask 1:start MyTask 2:start MyTask 0:end MyTask 3:start MyTask 1:end MyTask 4:start ... MyTask 9:end
3 個線程併發執行,任務完成後立即取下一個,體現線程復用。
| 機制 | 實現細節 |
|---|---|
| 任務入隊 | synchronized performTask() + notify() 喚醒單線程 |
| 任務出隊 | synchronized getTask() + wait() 釋放鎖阻塞 |
| 立即關閉 | interrupt() 遍歷 ThreadGroup 中斷所有線程 |
| 優雅關閉 | enumerate() 獲取活動線程 → 逐個 join() 等待 |
| 異常處理 | uncaughtException() 捕獲任務異常,防止工作線程意外死亡 |
直接使用 Executors 工廠或自定義 ThreadPoolExecutor,配置合理的隊列長度、拒絕策略(如 CallerRunsPolicy)、線程命名(ThreadFactoryBuilder),並接入監控(隊列積壓、活躍線程數、任務耗時)。