4

java | 线程池创建原则

 1 year ago
source link: https://benpaodewoniu.github.io/2023/01/02/java163/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
  • 过小导致程序不能充分利用系统资源,发生饥饿
  • 过大会导致更多的线程上下文切换,占用更多的内存

CPU 密集型计算

通常采用 cpu 核数 +1 能够实现最优 CPU 利用率。

+1 是保证当前线程由于页缺失故障(操作系统)或其他原因导致暂停时,额外的这个线程就能顶上去,保证 CPU 时钟周期不被浪费。

I/O 密集型运算

CPU 不总是处于繁忙状态。

线程数 = 核数 * 期望 CPU 利用率 * 总时间(CPU 计算时间 + 等待时间)/ CPU 计算时间

例如,4CPU 计算时间为 50%,其他等待时间为 50%,期望 CPU100% 利用率

4 * 100% * 100% / 50% = 8

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK