推荐直接使用 ThreadPoolExecutor 而非 Executors,因其可显式配置核心线程数、最大线程数、队列容量和拒绝策略,避免 newCachedThreadPool 和 newFixedThreadPool 因无界队列导致的 OOM 风险。
ThreadPoolExecutor 是 Java 中最核心的线程池实现类,它提供了对线程池的精细控制能力,比 Executors 工厂方法更透明、更安全。
Executors 提供的 newFixedThreadPool、newCachedThreadPool 等方法虽然写起来简单,但存在隐患:比如 newCachedThreadPool 使用无界队列(SynchronousQueue 实际上是“无缓冲”但配合无限线程数),可能引发 OOM;newFixedThreadPool 使用无界 LinkedBlockingQueue,任务堆积时内存持续增长。而 ThreadPoolExecutor 让你明确指定核心线程数、最大线程数、队列容量、拒绝策略等关键参数,避免隐式风险。
构造函数有 7 个参数,最常用的是以下 4 个核心组合:
建议自定义以统一命名和设置守护属性下面是一个生产环境推荐的写法:
ThreadFactory namedFactory = r -> {
Thread t = new Thread(r, "biz-task-pool-" + System.currentTimeMillis());
t.setDaemon(false);
return t;
};
BlockingQueue queue = new ArrayBlockingQueue<>(1024);
ThreadPoolExecutor executor = new ThreadPoolExecutor(
4, // corePoolSize
16, // maximumPoolSize
60L, // keepAliveTime
TimeUnit.SECONDS,
queue,
namedFactory,
new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝时由调用方执行,缓解压力
);
// 可选:启用线程池统计(如通过 JMX 或 Micrometer)
executor.prestartAllCoreThreads(); // 预热,避免首次任务延迟
基本上就这些。用好 ThreadPoolExecutor 不复杂,但容易忽略边界和生命周期管理。