当前位置:网站首页>认识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,忽略最早的任务(最先加入队列的任务)。
- 自定义拒绝策略
边栏推荐
- Qualcomm WLAN framework learning (30) -- components supporting dual sta
- Docker镜像的缓存特性和Dockerfile
- Redis入门完整教程:慢查询分析
- C language to quickly solve the reverse linked list
- Summary of wechat applet display style knowledge points
- Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
- Redis getting started complete tutorial: Key Management
- 解决无法通过ssh服务远程连接虚拟机
- CTF競賽題解之stm32逆向入門
- [Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
猜你喜欢
随机推荐
Compare two vis in LabVIEW
Redis: redis configuration file related configuration and redis persistence
A complete tutorial for getting started with redis: getting to know redis for the first time
Recommended collection: build a cross cloud data warehouse environment, which is particularly dry!
Editplus-- usage -- shortcut key / configuration / background color / font size
C语言快速解决反转链表
List related knowledge points to be sorted out
MariaDB的Galera集群-双主双活安装设置
Photoshop批量给不同的图片添加不同的编号
MariaDB的Galera集群应用场景--数据库多主多活
Redis getting started complete tutorial: Geo
Redis introduction complete tutorial: Collection details
Servlet服务器端和客户端中文输出乱码问题
SHP data making 3dfiles white film
数据库基础知识
Redis introduction complete tutorial: client communication protocol
Redis入门完整教程:Redis Shell
【爬虫】数据提取之xpath
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
Redis introduction complete tutorial: List explanation