线程池是Java并发编程中的核心组件,Java通过Executors类提供了几种常用的线程池实现,每种线程池适用于不同的场景。以下是主要的线程池种类:
1. 固定大小线程池 (FixedThreadPool)
创建方式:
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(int nThreads);
特点:
-
线程数量固定不变
-
使用无界工作队列(LinkedBlockingQueue)
-
适用于负载较重的服务器,为了资源分配的可预测性
适用场景:
-
需要限制并发线程数量的场景
-
CPU密集型任务(线程数通常设置为CPU核心数+1)
2. 可缓存线程池 (CachedThreadPool)
创建方式:
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
特点:
-
线程数量几乎无限制(最大为Integer.MAX_VALUE)
-
空闲线程默认60秒后回收
-
使用同步移交队列(SynchronousQueue)
-
线程数随负载自动增减
适用场景:
-
大量短生命周期的异步任务
-
IO密集型任务(如HTTP请求处理)
3. 单线程线程池 (SingleThreadExecutor)
创建方式:
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
特点:
-
只有一个工作线程
-
使用无界工作队列
-
保证任务顺序执行
适用场景:
-
需要任务顺序执行的场景
-
不需要并发但需要异步执行的场景
4. 定时/周期性线程池 (ScheduledThreadPool)
创建方式:
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(int corePoolSize);
特点:
-
可安排任务延迟执行或定期执行
-
核心线程数固定,但非核心线程数无限制(闲置时立即回收)
-
使用延迟工作队列(DelayedWorkQueue)
适用场景:
-
定时任务
-
周期性任务(如心跳检测)
示例
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPoolDemo {
public static void main(String[] args) {
// 创建一个包含3个线程的定时线程池
ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(3);
System.out.println("任务开始调度,当前时间: " + System.currentTimeMillis());
// 示例1: 延迟执行一次的任务
scheduledExecutor.schedule(() -> {
System.out.println("延迟5秒执行的任务,执行时间: " + System.currentTimeMillis());
}, 5, TimeUnit.SECONDS);
// 示例2: 固定速率周期性任务(不考虑任务执行时间)
scheduledExecutor.scheduleAtFixedRate(() -> {
System.out.println("固定速率任务,开始时间: " + System.currentTimeMillis());
try {
// 模拟任务执行耗时
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("固定速率任务,结束时间: " + System.currentTimeMillis());
}, 1, 3, TimeUnit.SECONDS); // 初始延迟1秒,之后每3秒执行一次
// 示例3: 固定延迟周期性任务(考虑任务执行时间)
scheduledExecutor.scheduleWithFixedDelay(() -> {
System.out.println("固定延迟任务,开始时间: " + System.currentTimeMillis());
try {
// 模拟任务执行耗时
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("固定延迟任务,结束时间: " + System.currentTimeMillis());
}, 1, 3, TimeUnit.SECONDS); // 初始延迟1秒,每次任务结束后延迟3秒再执行下一次
// 让主线程等待一段时间,以便观察定时任务执行
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭线程池
scheduledExecutor.shutdown();
System.out.println("线程池已关闭");
}
}
实际应用场景示例
// 模拟一个心跳检测任务
public class HeartbeatChecker {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public void startHeartbeat() {
scheduler.scheduleAtFixedRate(() -> {
try {
boolean isHealthy = checkServiceHealth();
System.out.println("服务健康状态: " + (isHealthy ? "正常" : "异常"));
if (!isHealthy) {
// 处理异常情况
handleUnhealthyService();
}
} catch (Exception e) {
System.err.println("心跳检测异常: " + e.getMessage());
}
}, 0, 10, TimeUnit.SECONDS); // 立即开始,每10秒检测一次
}
private boolean checkServiceHealth() {
// 实际的服务健康检查逻辑
return Math.random() > 0.2; // 模拟80%的概率返回健康
}
private void handleUnhealthyService() {
// 处理不健康状态的逻辑
System.out.println("警告:服务不健康,正在尝试恢复...");
}
public void stop() {
scheduler.shutdown();
}
public static void main(String[] args) throws InterruptedException {
HeartbeatChecker checker = new HeartbeatChecker();
checker.startHeartbeat();
// 运行一段时间后停止
Thread.sleep(60000);
checker.stop();
}
}
5. 自定义线程池 (ThreadPoolExecutor)
创建方式:
ThreadPoolExecutor customPool = new ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler
);