当前位置:网站首页>Why do I need a thread pool? What is pooling technology?
Why do I need a thread pool? What is pooling technology?
2022-06-09 04:55:00 【PHP Development Engineer 】
stay Java In language , There are two ways to improve the execution efficiency of the program , One is to use threads 、 The other is to use thread pool . And in a production environment , We usually use the latter . Why is this so ? Today, let's talk about the advantages of thread pool , And pool technology and its application .
1. Pool technology
Pool technology refer to Prepare in advance Some resources , You can Reuse These pre prepared resources . There are two main advantages of pooling technology : Prepare and reuse in advance . With Java Object creation in language as an example , When creating an object, you need to go through the following steps :
- according to new Parameter after identifier , Find the symbolic reference of the class in the constant pool ;
- If no symbol application is found ( Class is not loaded ), Load the class 、 analysis 、 Initialization etc. ;
- The virtual machine allocates memory for objects in the heap , And initialize the allocated memory to 0, For object headers , Establish the corresponding description structure ( The long-running : You need to find the free area in the heap , Modify memory allocation status, etc );
- Invokes the initialization method of the object ( The long-running : User's complex logic verification and other operations , Such as IO、 Whether the numerical calculation meets the regulations, etc ).
From the above process, we can see , Creating a class requires complex and time-consuming operations , therefore We should try to reuse existing classes , To ensure the efficient operation of the program , Of course, it would be great if you could create these classes in advance , The realization of these functions depends on pooling technology .
2. Application of pooling Technology
Common pool technology applications are : Thread pool 、 Memory pool 、 Database connection pool 、HttpClient Connection pool, etc , Next , Let's look at .
2.1 Thread pool
The principle of thread pool is very simple , Similar to the concept of buffer in the operating system . A number of threads will be started in the thread pool , These threads are all asleep . When the client has a new request , It will wake up a sleeping thread in the thread pool , Let it handle the request on the client side , When the request is processed , The thread is in sleep again . Thread pool can greatly improve the performance of programs . For example, there is a bank network center with a large concentration of provincial data , The number of concurrent client requests per second during peak periods exceeds 100, If you create a new thread for each client request , The cost of CPU Time and memory are amazing , If you take a possession 200 A pool of threads , That will save a lot of system resources , Make more CPU Time and memory are used to deal with practical business applications , Instead of frequent thread creation and destruction .

2.2 Memory pool
How to better manage application memory usage , At the same time, improve the frequency of memory use , This is a question worth pondering by every developer . Memory pool (Memory Pool) It provides a more feasible solution . During the creation of memory pool , Will allocate enough memory in advance , Form a preliminary memory pool . Then every time the user requests memory , It will return a piece of free memory in the memory pool , And set the flag of this memory as used . When the memory is used up and the memory is released , Not really free or delete The process of , It's the process of putting memory back into the memory pool , And in the process of putting it back, the flag should be set to idle . Last , When the application ends, the memory pool will be destroyed , Release each piece of memory in the memory pool . Advantages of memory pool :
- Reduce memory fragmentation , This advantage can be seen in the process of creating memory pools , When we are creating a memory pool , All the allocated memory blocks are relatively regular memory blocks , Reduce memory fragmentation .
- Increased memory usage . This can be seen from the process of allocating and freeing memory . Each allocation and release is not to call the functions or operators provided by the system to operate the actual memory , Instead, reuse the memory in the memory pool .
Disadvantages of memory pool : It's a waste of memory , Because to use the memory pool, you need to allocate a large piece of idle memory at the beginning , And not all of this memory is used .
2.3 Database connection pool
The basic idea of database connection pool is to store the database connection as an object in memory during system initialization , When the user needs to access the database , It's not about building a new connection , Instead, take an established free connection object out of the connection pool . After use , The user is not closing the connection , Instead, put the connection back into the connection pool , For next request access , And the establishment of these connections 、 Disconnection is managed by the connection pool itself . meanwhile , You can also set the parameters of the connection pool to control the initial number of connections in the connection pool 、 The upper and lower limits of connections and the maximum number of uses of each connection 、 Maximum free time, etc . Of course , You can also monitor the number of connections through the management mechanism of the connection pool itself 、 Usage, etc .

2.4 HttpClient Connection pool
HttpClient We often use it for HTTP Service access . In our project, there will be a function to obtain the execution status of tasks HttpClient, Ask once a second , Often Conection Reset abnormal . After analysis, we found that , The problem is HttpClient Each request will create a new connection , When the frequency of creating connections is higher than that of closing connections , It will lead to a large number of problems in the system TIME_CLOSED State connection , At this time, using connection pool to reuse connections can solve this problem .
3. Introduction to thread pool
Thread pool is a mode used by threads , It separates the concepts of thread and task , Use threads to perform tasks , It also provides a unified implementation method of thread management and task management , Avoid the performance overhead caused by frequent creation and destruction of threads .
4. Analysis of the advantages of thread pool
Compared with threads, thread pool , It does not require frequent creation and destruction of threads , Once the thread is created , By default, it will remain in the thread pool , When a mission comes , Then use these existing threads to perform tasks , As shown in the figure below :

advantage 1: Reuse threads , Reduce resource consumption
When creating a thread, you need to open up the virtual machine stack 、 Native Method Stack 、 Memory space of private threads such as program counters , And when they are destroyed, they have to recycle these private space resources , As shown in the figure below :

After the thread pool creates a thread, it will be placed in the thread pool , Therefore, compared with threads, thread pool , The first advantage is You can reuse threads 、 Reduce the consumption of system resources .
advantage 2: Improve response time
Thread pool is to reuse existing threads to perform tasks , Threads are created only when there are tasks , So compared to threads , Thread pool can respond to and execute tasks faster .
advantage 3: Control the number of threads and tasks
Thread pool provides more management functions , Here, the management function is mainly reflected in the following two aspects :
- Control the maximum concurrent number : The thread pool can create a fixed number of threads , This avoids the problem of unlimited thread creation . When too many threads are created , Will cause the system to execute slowly , because CPU The number of cores is certain 、 The number of tasks that can be processed at the same time is also certain , When there are too many threads, it will cause malicious contention and frequent thread switching , This causes the program to execute slowly , Therefore, the appropriate number of threads is the key to high-performance operation .
- Control the maximum number of tasks : If there are unlimited tasks , When there is not enough memory , It will cause the program execution to report an error , The thread pool can control the maximum number of tasks , When the task exceeds a certain number , Will use the rejection strategy to deal with the extra tasks , So as to ensure the healthy operation of the system .
advantage 4: More enhancements
Thread pools provide more functionality than threads , Such as regular execution and periodic execution .
summary
Pooling technology refers to preparing some resources in advance , These pre prepared resources can be reused when needed . There are two main advantages of pooling technology : Prepare and reuse in advance . Thread pool is a typical scenario of pooling technology , The main advantages of thread pool are 4 spot :1. Reuse threads , Reduced resource consumption ;2. Improve response time ;3. It provides the ability to manage the number of threads and tasks ;4. More enhancements .
Last
If you think this article is of little help to you , Point a praise . Or you can join my development communication group :1025263163 Learn from each other , We will have professional technical questions and answers
If you think this article is useful to you , Please give our open source project a little bit star:http://github.crmeb.net/u/defu Thank you for !
PHP Learning manual :https://doc.crmeb.com
Technical exchange forum :https://q.crmeb.com
边栏推荐
- Faster RCNN
- 软键盘出现搜索
- [006] [esp32 Development notes] burn firmware steps Using Flash Download Tool
- openGL_ 04 drawing with index array
- Brain: an interpretable deep learning framework for Alzheimer's disease (AD) classification
- API 網關 Apache APISIX 在 AWS Graviton3 上的安裝與性能測試
- OpenGL 01 - créer une fenêtre
- [004] [ESP32开发笔记] 音频开发框架ADF环境搭建——基于ESP-IDF
- R language multivariable generalized orthogonal GARCH (go-garch) model for fitting and forecasting high-dimensional volatility time series of stock market
- Typescript learning [9] generic
猜你喜欢
![[004] [esp32 Development Notes] audio development framework ADF environment construction - based on esp-idf](/img/55/9eb286bc56ec991837fc014b42fc20.png)
[004] [esp32 Development Notes] audio development framework ADF environment construction - based on esp-idf

openGL_01-創建窗口

Applets have become a must for super apps, competing for private domain "reserve"

“迪文杯”湖南文理学院电子设计大赛圆满结束

记录一次将dmp文件导入oracle数据库(本地导线上),所遇到的问题及解决方法

Wuqi_ New progress in vision language navigation: pre training and sim2real

Faster RCNN

Talk about 10 tips to ensure thread safety

三方账号授权登录系统设计思路

ps如何给图像加白边
随机推荐
API 网关 Apache APISIX 在 AWS Graviton3 上的安装与性能测试
Nacos1.1.4 local source code startup
Question bank and answers of G3 boiler water treatment examination in 2022
Mécanisme de mise en cache dans le transformateur
2022 test question bank and simulation test of special operation certificate for installation, maintenance and demolition at heights
Where will the money go in the SaaS industry in 2022?
2022 safety officer-a certificate examination questions and online simulation examination
Smart curly bracket escape
Quickly detect high-risk vulnerabilities of common middleware and components in penetration testing
Golang ---image-- overlap of thermal maps and photos
2022安全员-C证考试练习题模拟考试平台操作
宇邦新材深交所上市:市值47亿 第一季扣非净利降18%
openGL_01-创建窗口
Brain: an interpretable deep learning framework for Alzheimer's disease (AD) classification
迪文圆形智能屏上市
openGL_ 02 point line surface triangle
2022 R2 mobile pressure vessel filling test simulation 100 questions and simulation test
TypeScript 的内置对象
openGL_ 05 simple application of shader
Applets have become a must for super apps, competing for private domain "reserve"
