当前位置:网站首页>[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
边栏推荐
- The games that you've tasted
- Chapter 2 process management
- C language learning log 10.5
- Search DFS and BFS
- Section 5 - Operator details
- metaRTC4.0稳定版发布
- Clause 26: avoid overloading universal reference types
- OpenCV中的saturate操作(饱和操作)究竟是怎么回事
- [leetcode]- binary search
- Simple SR: best buddy Gans for highly detailed image super resolution
猜你喜欢

Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析

Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases

Advanced C - Section 3 - character functions and string functions

Explain the differences and usage scenarios between created and mounted

Advanced C - Section 2 - pointers

System file interface open

Recursion and recursion

Chapter 18 pagination: Introduction

QT brushes and brushes

MySQL8.0.13安装教程(有图)
随机推荐
Force deduction 121 questions
CMB written test graphical reasoning
C language learning log 11.7
shell变量学习笔记
Std:: Map initialization
Advantages of win8.1 and win10
Hidden implementation and decoupling, knowing Pimpl mode
Cesium:cesiumlab makes image slices and loads slices
Clause 48: understand template metaprogramming
Sub paragraph of Chapter 16
Explain the role of key attribute in V-for
String()和toString()方法得区别
使用EasyDarwin+FFmpeg实现rtsp推流
Chinese trumpet creeper
Sampo Lock
Ruoyi cloud startup tutorial (hand-held graphics)
Shell variable learning notes
Elliptic curve encryption
Chapter 2 process management
Regular expressions in QT