当前位置:网站首页>认识ThreadPoolExecutor
认识ThreadPoolExecutor
2022-07-04 23:01:00 【InfoQ】
线程池的创建
- FixedThreadPool:允许的请求队列长度为 Integer.MAX_VALUE,可能会堆积大量的请求,从而导致 OOM。
- SingleThreadPool:和FixedThreadPool会面临同样的问题
- CachedThreadPool :允许的创建线程数量为 Integer.MAX_VALUE,可能会创建大量的线程,从而导致 OOM。
- ScheduledThreadPool:和CachedThreadPool会面临同样的问题
public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
corePoolSize表示线程池的核心线程数。如果设置为0,则在没有任何任务时会销毁线程池;如果大于0,即使没有任务时也会保证线程池的线程数量等于此值。
maximumPoolSize表示线程池在任务增多时,可以创建的最大线程数。
keepAliveTime表示线程的存活时间,当线程池空闲时并且超过了此时间,多余的线程就会销毁,直到线程池中的线程数量销毁的等于 corePoolSize 为止,如果 maximumPoolSize 等于 corePoolSize,那么线程池在空闲的时候也不会销毁任何线程。
unit表示存活时间的单位
workQueue表示线程池执行的任务队列,当线程池的所有线程都在处理任务时,如果来了新任务就会缓存到此任务队列中排队等待执行。
threadFactory表示线程的创建工厂
提交线程任务
- submit()
- execute()
任务拒绝策略
- AbortPolicy,终止策略,线程池会抛出异常并终止执行,它是默认的拒绝策略;
- CallerRunsPolicy,把任务交给当前线程来执行;
- DiscardPolicy,忽略此任务(最新的任务);
- DiscardOldestPolicy,忽略最早的任务(最先加入队列的任务)。
- 自定义拒绝策略
边栏推荐
- 【taichi】用最少的修改将太极的pbf2d(基于位置的流体模拟)改为pbf3d
- Redis getting started complete tutorial: publish and subscribe
- Principle of lazy loading of pictures
- A complete tutorial for getting started with redis: transactions and Lua
- Redis introduction complete tutorial: Collection details
- 该如何去选择证券公司,手机上开户安不安全
- cout/cerr/clog的区别
- ScriptableObject
- UML diagram memory skills
- 法国学者:最优传输理论下对抗攻击可解释性探讨
猜你喜欢

Redis:Redis消息的发布与订阅(了解)

位运算符讲解

ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果

【二叉树】节点与其祖先之间的最大差值

The caching feature of docker image and dockerfile

VIM editor knowledge summary

S32 Design Studio for ARM 2.2 快速入门

A complete tutorial for getting started with redis: transactions and Lua

Qt个人学习总结

Redis introduction complete tutorial: detailed explanation of ordered collection
随机推荐
Network namespace
Redis入门完整教程:Redis使用场景
One of the commonly used technical indicators, reading boll Bollinger line indicators
The difference between debug and release
A complete tutorial for getting started with redis: getting to know redis for the first time
头文件重复定义问题解决“C1014错误“
D3.js+Three. JS data visualization 3D Earth JS special effect
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
[try to hack] wide byte injection
Actual combat simulation │ JWT login authentication
Notepad++--编辑的技巧
HMS core machine learning service
Recommended collection: build a cross cloud data warehouse environment, which is particularly dry!
[Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
金融市场,资产管理与投资基金
Header file duplicate definition problem solving "c1014 error“
微信公众号解决从自定义菜单进入的缓存问题
Notepad++ -- editing skills
The Chinese output of servlet server and client is garbled
可观测|时序数据降采样在Prometheus实践复盘