当前位置:网站首页>线程池概述
线程池概述
2022-07-02 05:24:00 【右眸Remnant】
线程池参数
直接new ThreadPoolExecutor()对象,并且手动的指定对应的参数
corePoolSize:线程池的核心线程数量 线程池创建出来后就会 new Thread() 5个
maximumPoolSize:最大的线程数量,线程池支持的最大的线程数
keepAliveTime:存活时间,当线程数大于核心线程,空闲的线程的存活时间 8-5=3
unit:存活时间的单位
BlockingQueue<Runnable> workQueue:阻塞队列 当线程数超过了核心线程数据,那么新的请求到来的时候会加入到阻塞的队列中
new LinkedBlockingQueue<>() 默认队列的长度是 Integer.MAX 那这个就太大了,所以我们需要指定队列的长度
threadFactory:创建线程的工厂对象
RejectedExecutionHandler handler:当线程数大于最大线程数的时候会执行的淘汰策略
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5
, 100
, 10
, TimeUnit.SECONDS
, new LinkedBlockingQueue<>(10000)
, Executors.defaultThreadFactory()
, new ThreadPoolExecutor.AbortPolicy()
);
线程池执行流程
- 首先根据 corePoolSIze创建工作线程
- 当并发量超过核心线程数,将多余的任务放如任务队列
- 如果任务队列满了,判断并发量是否超过最大线程数量,如果没有创建新的线程执行任务;
- 当任务处理完成,超过核心线程数量的线程会根据TimeUtil,过期销毁
- 如果启动新线程后仍处理不了任务,多余的任务根据拒绝策略进行其他操作
JDK中提供的几种线程池
- newCachedThreadPool
带有缓存的线程池,会在需要线程资源的时候进行创建,注意这里的最大线程数是Integer.MAX_VALUE
线程休眠时间为60s,超过时间会进行销毁。
一般用于执行短生命周期的异步任务
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
- newSingleThreadExecutor
单例线程池,线程池中时刻保证工作线程为1, 串行执行任务。
不同于fixedPool, 他保证返回的Executor不会创建额外的线程
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
- newScheduledThreadPool
具有定时任务的线程池,能够在延迟一定时间或定期执行任务。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
// new ScheduledThreadPoolExecute()
// 创建一个延迟队列
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory);
}
边栏推荐
- Fabric.js 基础笔刷
- Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
- Record my pytorch installation process and errors
- Global and Chinese market of cell culture freezers 2022-2028: Research Report on technology, participants, trends, market size and share
- Fabric. JS right click menu
- Storage of data
- Ls1046nfs mount file system
- Pycharm breakpoint management: temporarily cancel some breakpoints + run directly to a line
- Financial portal related information
- Fabric. JS iText superscript and subscript
猜你喜欢

黑马笔记---Set系列集合

Fabric.js 居中元素

Mysql基础---查询(1天学会mysql基础)

Collectors. Groupingby sort

Fabric.js 将本地图像上传到画布背景

Fabric. JS upload local image to canvas background

黑马笔记---Map集合体系
![Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]](/img/78/a17034d4b77d5c0dbe741f84a8ecd7.jpg)
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]

centos8安裝mysql8.0.22教程

Fabric.js 激活输入框
随机推荐
centos8安装mysql8.0.22教程
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Principle and implementation of parallax effect
brew install * 失败,解决方法
The El cascader echo only selects the questions that are not displayed
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
Fabric.js 3个api设置画布宽高
Ls1046nfs mount file system
Visual Studio导入
删除排序数组中的重复项go语言实现
6. Network - Foundation
摆正元素(带过渡动画)
Youth training camp -- database operation project
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
Fabric. JS upload local image to canvas background
青训营--数据库实操项目
centos8安裝mysql8.0.22教程
画波形图_数字IC
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Exercise notes 13 (effective letter ectopic words)