当前位置:网站首页>认识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,忽略最早的任务(最先加入队列的任务)。
- 自定义拒绝策略
边栏推荐
- Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
- Google collab trample pit
- Recommended collection: build a cross cloud data warehouse environment, which is particularly dry!
- Actual combat simulation │ JWT login authentication
- [graph theory] topological sorting
- MariaDB的Galera集群-双主双活安装设置
- Redis getting started complete tutorial: Geo
- 法国学者:最优传输理论下对抗攻击可解释性探讨
- Editplus-- usage -- shortcut key / configuration / background color / font size
- D3.js+Three. JS data visualization 3D Earth JS special effect
猜你喜欢
vim编辑器知识总结
Actual combat simulation │ JWT login authentication
The initial arrangement of particles in SPH (solved by two pictures)
Qualcomm WLAN framework learning (30) -- components supporting dual sta
Observable time series data downsampling practice in Prometheus
Redis入门完整教程:列表讲解
ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
Redis getting started complete tutorial: Geo
Excel 快捷键-随时补充
Redis introduction complete tutorial: detailed explanation of ordered collection
随机推荐
【js】-【排序-相关】-笔记
Redis getting started complete tutorial: publish and subscribe
【剑指Offer】6-10题
ScriptableObject
[graph theory] topological sorting
OSEK标准ISO_17356汇总介绍
ffmpeg快速剪辑
初试为锐捷交换机跨设备型号升级版本(以RG-S2952G-E为例)
Notepad++ -- editing skills
PS style JS webpage graffiti board plug-in
Photoshop batch adds different numbers to different pictures
Application of machine learning in housing price prediction
The solution to the lack of pcntl extension under MAMP, fatal error: call to undefined function pcntl_ signal()
JS 3D explosive fragment image switching JS special effect
A complete tutorial for getting started with redis: Pipeline
MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class
法国学者:最优传输理论下对抗攻击可解释性探讨
Qt加法计算器(简单案例)
The caching feature of docker image and dockerfile
解决无法通过ssh服务远程连接虚拟机