当前位置:网站首页>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: explore the change of water area in the North Canal basin over the past 30 years [year by year]](/img/7b/b9ef76cee8b32204331a9c3c21b5c2.jpg)
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]

Visual studio import

Brew install * failed, solution

Fabric.js 居中元素

kmp思想及模板代码

idea开发工具常用的插件合集汇总

Gee series: Unit 4 data import and export in Google Earth engine

Nodejs (02) - built in module

Fabric.js 激活输入框

Nodejs (03) -- custom module
随机推荐
Fabric.js IText 上标和下标
Detailed explanation of Pointer use
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Two implementation methods of delay queue
Determine whether there is an element in the string type
Youth training camp -- database operation project
Fabric.js 渐变
Fabric.js 右键菜单
线程池批量处理数据
Fabric. JS gradient
Fabric. JS 3 APIs to set canvas width and height
How to make an RPM file
Usage record of vector
Pyechats 1.19 generate a web version of Baidu map
黑马笔记---Map集合体系
生成二维码
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
黑馬筆記---Set系列集合