8人参与 • 2025-12-09 • 阿里
在阿里巴巴的《java开发手册》中有明确规定:
【强制】线程池不允许使用
executors去创建,而是通过threadpoolexecutor的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。
阿里禁止使用executors并不是说这个类的方法本身有致命的bug,而是因为它隐藏了资源耗尽的风险,不符合大厂对于系统稳定性和可观测性的苛刻要求。
下面我们以newfixedthreadpool方法为例详细拆解一下它的缺陷以及阿里的考量。
首先,我们来看一下这个方法的源码:
public static executorservice newfixedthreadpool(int nthreads) {
return new threadpoolexecutor(nthreads, nthreads,
0l, timeunit.milliseconds,
new linkedblockingqueue<runnable>());
}从源码可以看到,它创建了一个线程池,其核心参数是:
nthreadsnew linkedblockingqueue<runnable>() 【这是问题的核心】linkedblockingqueue 在默认情况下(无参构造)是一个 无界队列。这意味着它的容量是 integer.max_value,理论上可以无限地向其中添加任务。
这会导致一个严重的问题:当任务提交的速度持续超过线程处理的速度时,任务会在无界队列中无限堆积。
这个“无限堆积”会引发一系列连锁反应:
1、内存耗尽(oom):
outofmemoryerror: java heap space。这会直接导致整个应用崩溃,而不是仅仅线程池不可用。2、延迟和响应缓慢:
3、问题定位困难:
禁止的原因正是基于上述缺陷,并上升到工程规范和架构层面:
1、规避风险,保证系统稳定性:
2、提倡资源管理的显式化:
executors 提供的工厂方法像是一个“黑盒”,它隐藏了 threadpoolexecutor 的关键参数(如队列类型和大小)。通过强制使用 threadpoolexecutor 构造函数,开发者必须显式地设置核心线程数、最大线程数、队列容量、拒绝策略等。这个过程迫使开发者去思考线程池的配置是否合理,加深对线程池运行机制的理解。3、需要合理的拒绝策略:
threadpoolexecutor 提供了几种内置策略,如:
abortpolicy(默认):抛出 rejectedexecutionexception,让调用者感知到异常。callerrunspolicy:让提交任务的线程自己去执行它,这相当于一种负反馈,能有效平缓任务提交速率。discardpolicy / discardoldestpolicy:丢弃任务。应该直接使用 threadpoolexecutor 来创建线程池,并为其设置一个有界的工作队列。
import java.util.concurrent.*;
public class safethreadpoolexample {
public static void main(string[] args) {
// 核心参数
int corepoolsize = 10;
int maximumpoolsize = 20;
long keepalivetime = 60l;
int queuecapacity = 1000; // 关键:设置队列容量
// 手动创建线程池
threadpoolexecutor executor = new threadpoolexecutor(
corepoolsize,
maximumpoolsize,
keepalivetime,
timeunit.seconds,
new linkedblockingqueue<>(queuecapacity), // 有界队列
new threadfactory() { // 可选:自定义线程工厂,便于命名和监控
@override
public thread newthread(runnable r) {
thread t = new thread(r, "myapp-thread-" + r.hashcode());
t.setdaemon(false);
return t;
}
},
new threadpoolexecutor.callerrunspolicy() // 关键:设置合适的拒绝策略
);
// 使用 executor 提交任务...
// executor.execute(() -> { ... });
// 优雅关闭
// executor.shutdown();
}
}| 特性/方式 | executors.newfixedthreadpool() | 手动 threadpoolexecutor |
|---|---|---|
| 队列类型 | 无界 (linkedblockingqueue) | 有界 (可配置,如 new linkedblockingqueue<>(capacity)) |
| 资源风险 | 高,可能导致内存溢出(oom) | 低,通过容量控制和管理 |
| 拒绝策略 | 初期无效,oom前相当于无拒绝 | 有效,队列满后触发,可预测系统行为 |
| 可观测性 | 差,队列长度无上限,监控困难 | 好,队列有界,便于监控队列堆积情况 |
| 阿里规范 | 禁止 | 推荐 |
结论:executors.newfixedthreadpool() 的主要缺陷在于其无界工作队列带来的内存溢出风险。阿里等大厂禁止使用主要是为了规避上述风险,同时培养开发者的资源边界意识和风险意识,强制通过 threadpoolexecutor 的显式配置来构建更健壮、更可控、更易于监控的线程池,从而保障大规模分布式系统的稳定性。
到此这篇关于为什么阿里禁止使用executors去创建线程池的文章就介绍到这了,更多相关阿里禁止使用executors创建线程池内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论