当前位置:网站首页>JUC concurrent programming foundation (9) -- thread pool
JUC concurrent programming foundation (9) -- thread pool
2022-07-24 06:04:00 【aMythhhhh】
Thread pool
Thread pool overview
Thread pool is a method based on Pool thought Tools for managing threads , It often appears on multi-threaded servers , such as MySQL
Pooling , seeing the name of a thing one thinks of its function , To maximize revenue and minimize risk , And the idea of managing resources together .
Because there are many short-term tasks , Therefore, there will be additional costs for the continuous creation and destruction of threads , In addition, too many threads will lead to excessive scheduling problems , And thread pool can Ensure full utilization of the kernel , There are other benefits , for example :
- ** Reduce resource consumption :** Reuse created threads through pooling , Reduce the cost of thread creation and destruction .
- ** Improve response time :** When the task arrives , Skipped the step of creating thread , It can be executed immediately .
- ** Improved thread manageability :** Threads are scarce resources , If unlimited creation , Not only does it consume system resources , It will also cause resource scheduling imbalance due to the unreasonable distribution of threads , Reduce the stability of the system . Uniform allocation is possible using thread pools 、 Tune and monitor .
- More and more powerful functions : Thread pool has scalability , Allow developers to add more functionality to it . For example, delay timed thread pool ScheduledThreadPoolExecutor, To allow tasks to be postponed or performed on a regular basis .
In a concurrent environment , The system does not know how many tasks need to be performed ( At any time ), I don't know how many resources to give , There are many problems :
- Always need to apply 、 Destroy resources and schedule resources , It may bring huge consumption .
- The system has no way to apply for resources all the time , May cause resource depletion .
- There are many resource applications , You can use this thread , Have some of that , Cannot manage the internal resource distribution , The stability is greatly reduced .
At this time, the thread pool is needed , stay Java The embodiment of ThreadPoolExecutor class .
Thread pool Architecture - overall design
ThreadPoolExecutor The inheritance relationship of class is :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-uuiDpEzO-1657634311522)(C:\Users\aMyth\AppData\Roaming\Typora\typora-user-images\image-20220712205556165.png)]](/img/d0/10f51aa0bc917f5ca5e00ec23aa0cc.png)
** Top level interface -Executor:** Decouple task Submission from task execution , Users don't need to focus on how to create threads , How to schedule , Just submit the running logic of the task to Executor Then you can .
** Interface -ExecutorService:** Added some features , It can be summarized as , Generate for asynchronous tasks Future Method , It also provides a method to control the thread pool , Such as stopping .
** abstract class -AbstractExecutorService:** Upper level abstract classes , Concatenate the processes that perform the task , To ensure the implementation of the lower layer, you only need to pay attention to one method of executing tasks .
** Implementation class -ThreadPoolExecutor:** Implement the most complex part of the run ,ThreadPoolExecutor On the one hand, it will maintain its own life cycle , On the other hand, manage threads and tasks at the same time , Make a good combination of the two to perform parallel tasks .
ThreadPoolExecutor Operation process :
Task submitted , Determine whether Execute now perhaps Reject task , Or let the task go Buffer queue Stay , Just these three states .
If you execute a task, apply for a thread , When Task execution After that , The thread will continue to acquire new tasks , When you can't get a new task , Thread recycling .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-m8ch6trH-1657634311525)(C:\Users\aMyth\AppData\Roaming\Typora\typora-user-images\image-20220712210710153.png)]](/img/59/eaea6ea8a4765e3cc356d655d0d2b7.png)
Thread pool - Life cycle management
The running state of the thread pool and the number of threads in the pool , There are no two variables , But with a 32 position Integer Stored , Through high 3 Bit to save the running state , low 29 Number of bit save threads , It can reduce the time when there are changes , You need to match the two variables all the time , Prevent lock resources from being occupied . Thread pools provide many ways to get the current running state 、 Number , It's all based on An operation Methods .
private static int runStateOf(int c) {
return c & ~CAPACITY; } // Calculate the current running state
private static int workerCountOf(int c) {
return c & CAPACITY; } // Count the current number of threads
private static int ctlOf(int rs, int wc) {
return rs | wc; } // Generated by state and number of threads ctl
private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
//ctl Is to save the variables of running state and quantity , adopt ctlof obtain , Put in the AtomicInteger Type variable

These are the five running states of the thread pool , The life cycle transformation is shown in the figure :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-C7URcyMH-1657634311544)(C:\Users\aMyth\AppData\Roaming\Typora\typora-user-images\image-20220712212058896.png)]](/img/08/39c4aa8e1c4c175784c34e510d73d8.png)
Thread pool - Task execution mechanism
Task scheduling
- First, check the running state of thread pool , If not RUNNING, Then refuse directly , The thread pool should be guaranteed in RUNNING In the state of .
- If workerCount < corePoolSize, Then create and start a thread to execute the newly submitted task .
- If workerCount >= corePoolSize, And the blocking queue in the thread pool is not full , Then add the task to the blocking queue .
- If workerCount >= corePoolSize && workerCount < maximumPoolSize, And the blocking queue in the thread pool is full , Then create and start a thread to execute the newly submitted task .
- If workerCount >= maximumPoolSize, And the blocking queue in the thread pool is full , The task is handled according to the rejection strategy , The default way is to throw exceptions directly .

Task buffer
Blocking queues (BlockingQueue) Is a queue that supports two additional operations . These two additional operations are : When the queue is empty , The thread that gets the element will wait for the queue to become non empty . When the queue is full , The thread storing the element waits for the queue to be available . Blocking queues are often used in producer and consumer scenarios , Producers are threads that add elements to the queue , Consumer is the thread that takes elements from the queue . A blocking queue is a container for producers to store elements , And consumers only take elements from containers .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-d45gzVgL-1657634311556)(C:\Users\aMyth\AppData\Roaming\Typora\typora-user-images\image-20220712212840484.png)]](/img/6a/78363d5c79d30f1879a28e7ee87936.png)
What are the specific blocking queues , Visible above 《 Blocking queues 》 part .
Mission application
There are two possibilities for task execution : One is that the task is executed directly by the newly created thread . The other is that the thread gets the task from the task queue and executes it , The first case only occurs when the thread is initially created , The second is in most cases of thread fetching tasks .
The thread needs to constantly fetch the task execution from the task cache module , Help thread get task from blocking queue , Realize the communication between thread management module and task management module . This part of the strategy consists of getTask Method realization , The execution process is shown in the figure below :

Mission rejection
When the task cache queue of thread pool is full , And the number of threads in the thread pool reaches maximumPoolSize when , You need to reject the task , Take a task rejection strategy , Protect thread pool .
By implementing RejectedExecutionHandler Interface to customize the rejection policy , It can also be used JDK The rejection policy provided .

Thread pool -Worker Thread management
Worker Threads
In order to master the state of threads and maintain the life cycle of threads , Designed the working thread in the thread pool Worker.
private final class Worker extends AbstractQueuedSynchronizer implements Runnable{
final Thread thread;//Worker Held threads
Runnable firstTask;// Initialization task , It can be for null
}
Worker This worker thread , Realized Runnable Interface , And hold a thread thread, An initialization task firstTask.thread It is used when calling the constructor ThreadFactory To create a thread , Can be used to perform tasks ;firstTask Use it to save the first incoming task , This task can have or can be null. If this value is not empty , Then the thread will execute this task immediately at the beginning of startup , It also corresponds to the situation when the core thread is created ; If the value is null, Then you need to create a thread to execute the task list (workQueue) The task , That is, the creation of non core threads .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OncAJXWd-1657634311570)(C:\Users\aMyth\AppData\Roaming\Typora\typora-user-images\image-20220712214507539.png)]](/img/5a/0104141bc4397aa59c36b09ca1c745.png)
Worker Thread increase
The increase of threads is through addWorker Method , The function of this method is to add a thread , This method does not consider the stage in which the thread pool is added , This strategy of allocating threads was completed in the last step , This step only completes adding threads , And make it run , Finally, it returns whether the result is successful .addWorker Method has two parameters :firstTask、core.firstTask Parameter is used to specify the first task executed by the new thread , This parameter can be empty ;core Parameter is true Indicates whether the current number of active threads is less than when a new thread is added corePoolSize,false Indicates whether the number of active threads is less than before adding a new thread maximumPoolSize, The execution process is shown in the figure below :

worker Thread recycling
Destroy dependency of thread in thread pool JVM Automatic recycling , The task of thread pool is to maintain a certain number of thread references according to the current state of thread pool , Prevent this part of the thread from being JVM Recycling , When the thread pool decides which threads need to be recycled , Just eliminate the references .
try {
while (task != null || (task = getTask()) != null) {
// Perform tasks
}
} finally {
processWorkerExit(w, completedAbruptly);// When the task is not available , Take the initiative to recycle yourself
}
worker Perform tasks
stay Worker Class run Method is called runWorker Method to perform the task ,runWorker The implementation process of the method is as follows :
1.while The cycle goes on and on getTask() Method fetch task . 2.getTask() Method to get the task from the blocking queue . 3. If the thread pool is stopping , Then make sure that the current thread is in an interrupt state , Otherwise, make sure that the current thread is not in an interrupt state . 4. Perform tasks . 5. If getTask The result is null And then jump out of the loop , perform processWorkerExit() Method , Destruction of the thread .
The execution process is shown in the figure below :

Reference resources :https://tech.meituan.com/2020/04/02/java-pooling-pratice-in-meituan.html
边栏推荐
- Delete the weight of the head part of the classification network pre training weight and modify the weight name
- CRC-16 MODBUS code
- JUC并发编程基础(4)--线程组和线程优先级
- Vsual studio 2013 environment UDP multicast
- Accurate calculation of time delay detailed explanation of VxWorks timestamp
- JS star scoring effect
- synergy局域网实现多主机共享键鼠(amd、arm)
- "Statistical learning methods (2nd Edition)" Li Hang Chapter 15 singular value decomposition SVD mind map notes and after-school exercise answers (detailed steps) SVD matrix singular value Chapter 15
- JUC并发编程基础(6)--Lock锁
- 绘制轮廓 cv2.findContours函数及参数解释
猜你喜欢

String methods and instances

【深度学习】手写神经网络模型保存

Common features of ES6

Synergy LAN realizes multi host shared keyboard and mouse (AMD, arm)

"Statistical learning methods (2nd Edition)" Li Hang Chapter 13 introduction to unsupervised learning mind map notes

Machine learning (Zhou Zhihua) Chapter 3 Notes on learning linear models

"Statistical learning methods (2nd Edition)" Li Hang Chapter 14 clustering method mind map notes and after-school exercise answers (detailed steps) K-means hierarchical clustering Chapter 14
![[activiti] process variables](/img/5e/34077833f6eb997e64f186d4773e89.png)
[activiti] process variables

通道注意力与空间注意力模块

Add se channel attention module to the network
随机推荐
MySql与Qt连接、将数据输出到QT的窗口tableWidget详细过程。
Numpy cheatsheet
GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。
[activiti] process variables
‘Results do not correspond to current coco set‘
Jupyter notebook选择conda环境
Official account development custom menu and server configuration are enabled at the same time
Conversion of world coordinate system, camera coordinate system and image coordinate system
JS star scoring effect
[MYCAT] MYCAT sets up read-write separation
Typora installation package in November 2021, the last free version of the installation package to download v13.6.1
[MYCAT] MYCAT installation
"Statistical learning methods (2nd Edition)" Li Hang Chapter 16 principal component analysis PCA mind map notes and after-school exercise answers (detailed steps) PCA matrix singular value Chapter 16
"Statistical learning methods (2nd Edition)" Li Hang Chapter 15 singular value decomposition SVD mind map notes and after-school exercise answers (detailed steps) SVD matrix singular value Chapter 15
Bat batch script, running multiple files at the same time, batch commands executed in sequence, and xshell script.
Chapter III summary of linear model
C语言链表(创建、遍历、释放、查找、删除、插入一个节点、排序,逆序)
找数组中出现次数最多的数
Unicast, multicast, broadcast, tool development, introduction to QT UDP communication protocol development and source code of development tools
Openwrt quick configuration Samba