当前位置:网站首页>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);
}
边栏推荐
- Centos8 installation mysql8.0.22 tutorial
- 记录sentry的踩坑之路
- The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
- Fabric.js 居中元素
- Fabric.js 渐变
- JVM class loading mechanism
- 【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
- Creation and destruction of function stack frames
- 简单封装 js并应用
- centos8安裝mysql8.0.22教程
猜你喜欢
idea开发工具常用的插件合集汇总
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
el form 表单validate成功后没有执行逻辑
Fabric. JS right click menu
Visual Studio导入
Gee series: unit 7 remote sensing image classification using GEE [random forest classification]
Fabric.js IText设置指定文字的颜色和背景色
Online music player app
Gee dataset: chirps pentad high resolution global grid rainfall dataset
随机推荐
6.网络-基础
7.1 Résumé du concours de simulation
在线音乐播放器app
"Original, excellent and vulgar" in operation and maintenance work
数据的储存
Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table
460. LFU 缓存 双向链表
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
Johnson–Lindenstrauss Lemma(2)
Fabric. JS right click menu
Essence and physical meaning of convolution (deep and brief understanding)
Fabric. JS centered element
Gee: remote sensing image composite and mosaic
Operator details
Visual Studio導入
Gee series: unit 8 time series analysis in Google Earth engine [time series]
There are duplicate elements in leetcode. Go implementation
Leetcode 18 problem [sum of four numbers] recursive solution
视差特效的原理和实现方法
Gee dataset: chirps pentad high resolution global grid rainfall dataset