当前位置:网站首页>Thread pool overview
Thread pool overview
2022-07-02 05:28:00 【Right eye remnant】
List of articles
Thread pool parameters
direct new ThreadPoolExecutor() object , And manually specify the corresponding parameters
corePoolSize: Number of core threads in thread pool After the thread pool is created, it will new Thread() 5 individual
maximumPoolSize: Maximum number of threads , The maximum number of threads supported by the thread pool
keepAliveTime: Survival time , When the number of threads is greater than the core thread , The lifetime of idle threads 8-5=3
unit: Units of survival time
BlockingQueue<Runnable> workQueue: Blocking queues When the number of threads exceeds the core thread data , Then new requests will be added to the blocked queue when they arrive
new LinkedBlockingQueue<>() The default queue length is Integer.MAX Then this one is too big , So we need to specify the length of the queue
threadFactory: Create the factory object of the thread
RejectedExecutionHandler handler: The elimination strategy that will be implemented when the number of threads is greater than the maximum number of threads
ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(5
, 100
, 10
, TimeUnit.SECONDS
, new LinkedBlockingQueue<>(10000)
, Executors.defaultThreadFactory()
, new ThreadPoolExecutor.AbortPolicy()
);
Thread pools execute processes
- First of all, according to the corePoolSIze Create worker threads
- When the concurrency exceeds the number of core threads , Put redundant tasks into task queue
- If the task queue is full , Determine whether the concurrency exceeds the maximum number of threads , If no new thread is created to execute the task ;
- When the task processing is completed , Threads that exceed the number of core threads will be based on TimeUtil, Expired destruction
- If the task cannot be processed after starting a new thread , Redundant tasks perform other operations according to the rejection policy
JDK Several thread pools provided in
- newCachedThreadPool
Thread pool with cache , It will be created when thread resources are needed , Note that the maximum number of threads here is Integer.MAX_VALUE
Thread sleep time is 60s, Over time, it will be destroyed .
It is generally used to execute asynchronous tasks with a short life cycle
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
- newSingleThreadExecutor
Singleton thread pool , Ensure that the working thread in the thread pool is 1, Serial execution task .
differ fixedPool, He promised to return Executor No additional threads will be created
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
- newScheduledThreadPool
Thread pool with scheduled tasks , Be able to perform tasks after a certain delay or on a regular basis .
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
// new ScheduledThreadPoolExecute()
// Create a delay queue
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue(), threadFactory);
}
边栏推荐
- Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- Financial portal related information
- 【技术随记-08】
- Determine whether there is an element in the string type
- php/js cookie共享跨域的问题
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- 金融门户相关信息
- 操作符详解
- Leetcode18题 【四数之和】递归解法
猜你喜欢
Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
Brew install * failed, solution
Differential identities (help find mean, variance, and other moments)
Gee series: Unit 4 data import and export in Google Earth engine
Fabric.js IText 上标和下标
青训营--数据库实操项目
Fabric. JS upload local image to canvas background
Collectors.groupingBy 排序
Gee: remote sensing image composite and mosaic
Fabric. JS gradient
随机推荐
Dark horse notes -- map set system
Fabric.js IText 手动设置斜体
Fabric. JS activation input box
中小型项目手撸过滤器实现认证与授权
视差特效的原理和实现方法
黑馬筆記---Set系列集合
指针使用详解
Fabric.js IText设置指定文字的颜色和背景色
Collectors.groupingBy 排序
Get the details of the next largest number
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Database batch insert data
Visual Studio导入
Nodejs (02) - built in module
Paddlepaddle project source code
MySQL foundation --- query (learn MySQL foundation in 1 day)
There are duplicate elements in leetcode. Go implementation
Go implements leetcode rotation array
Leetcode18题 【四数之和】递归解法
Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import