当前位置:网站首页>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 (2)
- DRF - paging, JWT introduction and principle, JWT quick use, JWT source code analysis, JWT custom return format, custom user issued token, custom token authentication class
- What are the skills of API interface optimization?
- R语言怎么学
- [basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)
- Data warehouse construction - DWT floor
- Application and principle of distributed current limiting redistribution rratelimiter
- Outlier detection and open set identification (2)
- Techo Hub 福州站干货来袭|与开发者共话工业智能新技术
- redis版本怎么查看(查看redis进程)
猜你喜欢

Error reporting: Rong Lianyun sends SMS verification code message 500

我不建议你使用SELECT *

Anomaly detection and unsupervised learning (2)

There is a span tag. If you want to do click events on it, how can you expand the click area

Dynamic programming problem (VII)

Api 接口优化有哪些技巧?

Teach you how to install latex (nanny level tutorial)
![[basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)](/img/0b/d6defd524f83e69b40b5878ffe2e3c.png)
[basic course of flight control development 8] crazy shell · open source formation uav-i2c (laser ranging)

AQS原理

How to solve the problem that the Oracle instance cannot be started
随机推荐
NPM run serve stuck at 40%
Camera Hal OEM module ---- CMR_ preview.c
数仓搭建——ADS层
R语言怎么学
MATLAB02:结构化编程和函数定义「建议收藏」
关于ThreadPool的一些注意事项
How to learn R language
I was asked several questions about string in the interview. Can you answer them?
Nftscan and nftplay have reached strategic cooperation in the field of NFT data
手把手教你安装Latex(保姆级教程)
Alibaba code index technology practice: provide reading experience of local IDE for code review
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
将Word中的表格以图片形式复制到微信发送
CDN mode uses vant components, and components cannot be called after they are introduced
Basic knowledge of PHP language (super detailed)
"Food alliance ordering system"
CUDA related
rk3399 9.0驱动添加Powser按键
PTA (daily question) 7-72 calculate the cumulative sum
Data warehouse construction - ads floor