当前位置:网站首页>Asynchronous mode worker thread
Asynchronous mode worker thread
2022-07-29 00:49:00 【51CTO】
1. Definition
Let limited worker threads (Worker Thread) To take turns processing an infinite number of tasks asynchronously . It can also be classified as the mode of division of labor , Its typical implementation is thread pool , It also reflects the Heyuan mode in the classic design pattern .
for example , Undersea fishing waiter ( Threads ), Take turns with each guest's order ( Mission ), If each guest is equipped with a dedicated waiter , Then the cost is too high ( Contrast another multithreading design pattern :Thread-Per-Message)
Be careful , Different task types should use different thread pools , It's a way to avoid starvation , And improve efficiency
for example , If a restaurant worker wants to greet guests ( Task type A), I'm going to cook in the back kitchen again ( Task type B) Obviously not very efficient , It's divided into waiters ( Thread pool A) With the chef ( Thread pool B) More reasonable , Of course, you can think of a more detailed division of labor
2. hunger
Fixed size thread pool will be hungry
● Two workers are two threads in the same thread pool
● What they're going to do is : Ordering for guests and cooking in the back kitchen , It's two stages of work
○ The guests order : You have to order first , Wait for the dishes to be ready , Serve , In the meantime, the workers handling the order have to wait
○ Back kitchen cooking : Nothing to say. , Just do it
● Like workers A I've taken care of the order , Next it has to wait Worker B Do the dishes well , And then the dishes , They also cooperated very well
● But now there are two guests at the same time , At this time, the workers A And workers B We've all gone to order , At this time, no one cooked , hunger
Output
17:21:27.883 c.TestDeadLock [pool-1-thread-1] - Handle order ...
17:21:27.891 c.TestDeadLock [pool-1-thread-2] - cook a dish
17:21:27.891 c.TestDeadLock [pool-1-thread-1] - Serve : Roast chicken wings
When the comment is cancelled , Possible outputs
17:08:41.339 c.TestDeadLock [pool-1-thread-2] - Handle order ...
17:08:41.339 c.TestDeadLock [pool-1-thread-1] - Handle order ...
The solution is to increase the size of the thread pool , But not the fundamental solution , As mentioned earlier , Different types of tasks , Use different thread pools , for example :
Output
17:25:14.626 c.TestDeadLock [pool-1-thread-1] - Handle order ...
17:25:14.630 c.TestDeadLock [pool-2-thread-1] - cook a dish
17:25:14.631 c.TestDeadLock [pool-1-thread-1] - Serve : Sauteed Potato, Green Pepper and Eggplant
17:25:14.632 c.TestDeadLock [pool-1-thread-1] - Handle order ...
17:25:14.632 c.TestDeadLock [pool-2-thread-1] - cook a dish
17:25:14.632 c.TestDeadLock [pool-1-thread-1] - Serve : Sauteed Chicken Dices with Chili Peppers
therefore , Be careful , Different task types should use different thread pools , It's a way to avoid starvation , And improve efficiency .
3. How many thread pools are appropriate to create
- Too small will lead to the program can not make full use of system resources 、 It's easy to lead to hunger
- Too much will lead to more thread context switching , Use more memory
3.1 CPU Intensive operations
Usually used cpu Check the number + 1 Be able to achieve optimal CPU utilization ,+1 Is to ensure that when the thread fails due to page loss ( operating system ) Or other reasons , The extra thread can be pushed up , Guarantee CPU Clock cycles are not wasted .
3.2 I/O Intensive operations
CPU Not always busy , for example , When you perform business calculations , At this time will use CPU resources , But when you execute I/O In operation 、 long-range RPC Invocation time , Including database operation , Now CPU I'm free , You can use multithreading to improve its utilization .
The empirical formula is as follows
Number of threads = Check the number * expect CPU utilization * Total time (CPU computing time + Waiting time ) / CPU computing time
for example 4 nucleus CPU The calculation time is 50% , Other waiting times are 50%, expect cpu By 100% utilize , To paraphrase formula :4 * 100% * 100% / 50% = 8
for example 4 nucleus CPU The calculation time is 10% , Other waiting times are 90%, expect cpu By 100% utilize , To paraphrase formula :4 * 100% * 100% / 10% = 40
4. Custom thread pool
边栏推荐
- Dynamic programming problem (4)
- AQS原理
- Soft test --- database (4) SQL statement
- Upload Excel files with El upload and download the returned files
- Common sparse basis and matlab code for compressed sensing
- Rk3399 9.0 driver add powser button
- 手把手教你安装Latex(保姆级教程)
- 靠云业务独撑收入增长大梁,微软仍然被高估?
- 刷题的第三十天
- 【MySQL 8】Generated Invisible Primary Keys(GIPK)
猜你喜欢

华为发布HarmonyOS 3.0,向“万物互联”再迈一步

"Food alliance ordering system"
![[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver](/img/06/5e417bb97e309b6ee27dc693cabb85.png)
[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver

“吃货联盟定餐系统”

手把手教你安装Latex(保姆级教程)

Requestvideoframecallback() simple instance

【飞控开发基础教程8】疯壳·开源编队无人机-I2C(激光测距)

靠云业务独撑收入增长大梁,微软仍然被高估?

MySQL sub database and sub table and its smooth expansion scheme

Download the latest version of visual studio code and connect to the server remotely (very detailed)
随机推荐
Outlier detection and Gan network (1)
[untitled]
C语言括号匹配(栈括号匹配c语言)
Download the latest version of visual studio code and connect to the server remotely (very detailed)
最长上升子序列
Dynamic programming problem (VIII)
Camera Hal OEM module ---- CMR_ preview.c
主线程与守护线程
Execute immediate simple sample set (DML)
How to solve the problems of MQ message loss, duplication and backlog?
zabbix部署及监控
17.机器学习系统的设计
会议OA项目之会议通知&会议反馈&反馈详情功能
About 1931cie -- conversion of XYZ color coordinate graph to RGB color coordinate relationship
Shell programming specifications and variables
Depth first search (DFS) and its matlab code
【无标题】
selenium对接代理与seleniumwire访问开发者工具NetWork
PTA (daily question) 7-73 turning triangle
Andriod6.0 low power mode (turn off WiFi, Bluetooth, GPS, screen brightness, etc.)