当前位置:网站首页>线程池大小的设置策略
线程池大小的设置策略
2022-04-22 12:01:00 【万猫学社】
线程池大小的设置策略
线程池需要设置合适的大小,假如设置的太大,线程上线文切换过于频繁,造成大量资源开销,反而会使性能降低。假如设置的太小,存在很多可用的处理器资源却未在工作,会造成资源的浪费和对吞吐量造成损失。
为了充分利用处理器资源,创建的线程数至少要等于处理器核心数。如果所有的任务都是计算密集型的,那么线程数等于可用的处理器核心数就可以了。不过,如果所有的任务都是IO密集型,那么处理器大部分时间是空闲的,所有要适当的增加线程数。线程等待时间所占比例越高,需要越多线程。线程运算时间所占比例越高,需要越少线程。于是可以使用下面的公式进行估算:
最佳线程数 = (1 + 线程等待时间/线程计算时间)* 目标CPU的使用率 * 处理器核心数
例如:平均每个线程计算运行时间为0.5s,而线程等待时间(非计算时间,比如IO)为1.5s,目标CPU的使用率是90%,CPU核心数为8,那么根据上面这个公式估算得到:(1 + 1.5/0.5) * 90% * 8 = 28.8。
即使有上面的简单估算方法,也许看似合理,但实际上也未必合理,都需要结合系统真实情况(比如是IO密集型或者是CPU密集型或者是纯内存操作)和硬件环境(CPU、内存、硬盘读写速度、网络状况等)来不断尝试达到一个符合实际的合理估算值,也可以尝试Dark Magic的估算方法。
版权声明
本文为[万猫学社]所创,转载请带上原文链接,感谢
https://cloud.tencent.com/developer/article/1985644
边栏推荐
- 在 Flutter 添加页面过渡动画
- How to set up LAN file sharing in win8 system?
- MySQL使用SELECT 语句不加ORDER BY默认是如何排序的?
- Leetcode Tencent selected exercises 50 questions - 104 Maximum depth of binary tree
- Order details page
- What happens when you run the NPM install command?
- Kernel pwn 基础教程之 Heap Overflow
- C language DLL Dynamic Link Library
- 互联网求职卷中卷,应届生怎样才能杀出重围
- LeetCode383. Ransom letter
猜你喜欢

Open new space for development with digital key

《假如你身处被“科技制裁”的俄罗斯》之GBase篇

Chapter 1 Introduction

POSTGRESQL 15's new function is worth looking forward to, two of which make complaints about it for a long time.

塔米狗项目解读|北京华隆典当有限责任公司49.5%股权转让

恭喜!您已关注公众号满1年, 诚邀您免费加入网易数据分析培训营!

Developer friendly public chain Neo | how to connect web2 developers to Web3 world

CVPR2022 | 移动端手部三维重建

阿里实习offer成功上岸,这几点至关重要

游戏+NFT,脱虚向实外的另一可行场景
随机推荐
synchronized实现和原理分析
云中漫步-我这一辈子
第一章 入门概述
MySQL之父:代码应该一次写成,而不是后面再改
Experience and guidance of senior students preparing for the postgraduate entrance examination of English translation of Southeast University in 2023
互联网求职卷中卷,应届生怎样才能杀出重围
费用详情页面
What happens when you run the NPM install command?
360数科等入选信通院首批数据安全管理能力认证公司
PTC: major change in ESG product R & D of construction machinery
2021-09-17
POSTGRESQL 15 的新功能,值得期待,其中两个被吐槽很久
Yunrong technology joined the dragon dragon dragon community to help the digital transformation of the financial industry
《假如你身处被“科技制裁”的俄罗斯》之GBase篇
Game + NFT, another feasible scene from virtual to real
31. Create two identical objects
POSTGRESQL 15's new function is worth looking forward to, two of which make complaints about it for a long time.
Intelligent party building integrated management platform development, digital party building integrated management system
2019-8-8-wpf - touch and mouse click response in non customer area
LeetCode202. Happy number