当前位置:网站首页>[multithreading] thread pool core class -threadpoolexecutor
[multithreading] thread pool core class -threadpoolexecutor
2022-06-13 05:04:00 【A Xiaobing】
Preface
ThreadPoolExecutor There are many constructors , Let's explain the method with the most parameters
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)
Parameters on
1、 First of all, let's introduce corePoolSize、maximumPoolSize、workQueue These are the three input parameters
- corePoolSize: Number of core threads
- maximumPoolSize: Maximum number of threads
- workQueue: Blocking queues , Store tasks waiting to be performed , This parameter is very important , Let's talk about it later
These three relationships :
- If the running thread is less than corePoolSize, A new thread will be created directly to handle the task , Even if the other threads in the thread pool are idle
- If the running thread is greater than or equal to corePoolSize, And less than maximumPoolSize, So at this point , as long as workQueue When it's full , Will create a new thread to handle the task
- If the corePoolSize=maximumPoolSize, The size of the thread pool is fixed , If this time , There are new tasks submitted and workQueue If it's not full , Will put the request in workQueue in , Waiting for idle threads , And then from workQueue Take out the task for processing
- If the number of threads running is greater than maximumPoolSize, meanwhile workQueue Is already full , The policy parameters will be rejected handler To develop processing strategies
1.1、 These three parameters are used to process tasks as follows :
When submitting a new task to the thread pool , The thread pool will determine the processing method of the task according to the number of running threads in the current thread pool , The main way to deal with it is : Switch directly 、 Use infinite queues 、 Use bounded queues
- Switch directly : The common queue is SynchronousQueue
- Using infinite queues is using queues based on linked lists , such as LinkedBlockingQueue, In this way , The maximum number of threads in the thread pool is corePoolSize, here maximumPoolSize It doesn't work , When all threads in the thread pool are running and submitting tasks , Will be put in the waiting queue
- Using bounded queues is ArrayBlockingQueue, In this way , You can limit the maximum number of threads in the thread pool to maximumPoolSize, This can achieve low consumption , There are some difficulties in this way , It is difficult for the thread pool to schedule threads , So the capacity of thread pool and queue is limited
1.2、 Through these three parameters , To reduce system resource consumption :
- If you want low consumption , You can set a larger queue capacity and a smaller line city capacity , This will reduce the throughput of the thread processing tasks
- If the submitted task is often blocked , You can set the maximum number of threads , Reset the maximum number of threads in the thread pool
- If the capacity of the queue is small , In general, you need to set the capacity of the thread pool a little larger , In this case ,CPU The usage rate will be higher
- If the thread pool has a large capacity , The amount of concurrency will increase , You need to consider the problem of thread scheduling , Instead, it may reduce the throughput of processing tasks
2、 Other parameters into :keepAliveTime、unit、threadFactory、handler
- keepAliveTime: How long does the thread last until it has no task to execute , When the number of threads in the thread pool is greater than corePoolSize When , No new tasks have been submitted , Threads outside the core thread will not be destroyed immediately , But wait for a while , If time goes beyond keepAliveTime Time set , It will self-destruct
- unit:keepAliveTime Time unit of
- threadFactory: Thread factory , Used to create threads By default, a default factory is provided to create threads , When using the default factory to create threads , This will give the newly created thread the same priority , And is unguarded Threads , It also sets the name of the thread
- handler: Refusal to process tasks , If workQueue The blocking queue is full , And there is no free thread pool , here , Continue to submit tasks , A strategy is needed to deal with this task .
3、handler Four strategies provided
- AbortPolicy: Throw an exception directly , The default configuration
- CallerRunsPolicy: Use the thread of the caller to execute the task
- DiscardOldestPolicy: Discard the top task in the queue and execute the current task
- DiscardPolicy: Discard the current task directly
Provides methods for starting and stopping tasks
- execute(): Submit tasks , To the thread pool to execute
- submit(): Submit tasks , Able to return execution results execute+Future
- shutdown(): Close thread pool , Wait for the task to finish
- shutdownNow(): Close thread pool now , Don't wait for the task to finish
Methods provided for monitoring
- getTaskCount(): Total number of tasks executed and not executed by thread pool
- getCompletedTaskCount(): Number of tasks completed
- getPoolSize(): Current number of threads in thread pool
- getCorePoolSize(): Number of core threads in thread pool
- getActiveCount(): The number of threads currently executing tasks in the thread pool
边栏推荐
- Simple SR: best buddy Gans for highly detailed image super resolution
- [JS solution] leedcode 117 Populate the next right node pointer II for each node
- Luogu p1036 number selection
- Clause 31: avoid default capture mode
- PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
- 【多线程编程】Future接口获取线程执行结果数据
- Search DFS and BFS
- Clause 27: alternatives to overloading with familiar universal reference types
- Several methods of identifying equivalent circuit of circuit drawing
- Kaggle 时间序列教程
猜你喜欢
Configuration used by automatic teaching evaluation script
BM1Z002FJ-EVK-001开机测评
metaRTC4.0集成ffmpeg编译
CMB written test graphical reasoning
Solution to sudden font change in word document editing
Binary search and binary answer
Section 2 - branch and loop statements
Section 3 - functions
Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
随机推荐
C language learning log 12.14
Section 6 - pointers
Elliptic curve encryption
C language learning log 10.10
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
关于匿名内部类
Bomb disposal cat
【多线程编程】Future接口获取线程执行结果数据
The games that you've tasted
Luogu p3654 fisrt step
Std:: Map empty example
Stepping on a horse (one stroke)
LeetCode第297场周赛(20220612)
Clause 27: alternatives to overloading with familiar universal reference types
C language learning log 2.19
【转载】C语言内存和字符操作函数大全
[leetcode]- binary search
Gradient descent, learning rate
Clause 30: be familiar with the failure of perfect forwarding
Section 2 - branch and loop statements