问题描述
场景:我有一个示例应用程序,我有 3 个不同的系统配置 -
Scenario : I have a sample application and I have 3 different system configuration -
- 2 core processor, 2 GB RAM, 60 GB HHD,
- 4 core processor, 4 GB RAM, 80 GB HHD,
- 8 core processor, 8 GB RAM, 120 GB HHD
为了有效地利用我的应用程序的 H/W 功能,我希望配置 no.应用程序级别的线程数.但是,我希望只有在彻底了解系统功能后才能这样做.
In order to effectively exploit the H/W capabilities for my application, I wish to configure the no. of threads at the application level. However, I wish to do this only after a thorough understanding of system capabilities.
是否有某种方法(系统/模式/工具)可以参考最大和最小编号来确定系统实力.它可以优化服务的线程数没有任何效率和性能损失.通过这种方式,我可以只为我的应用程序配置那些能够充分利用相应硬件配置并实现最佳性能的值.
Could there be some way(system/modus/tool) to determine the system prowess with reference to the max and min no. of threads it could service optimally & without any loss in efficiency and performance. By this, I could configure only those values for my application that will do full justice and achieve best performance for the respective hardware configuration.
已编辑 1 :任何人都可以就如何为特定的硬件配置设置基线提出建议.
Edited1 :Could any one please advise any read-up on how to set a baseline for a particular h/w config.
已编辑 2 :为了让它更直接 - 希望了解/了解我可以阅读的任何资源/文章,以便在一般/整体级别对线程的 CPU 管理有一些了解.
Edited2 :To make it more direct - Wish to learn/know about any resource/write-up that I can read to gain some understanding on CPU management of Threads at a general/holistic level.
推荐答案
要使用的最佳线程数取决于多个因素,但主要取决于可用处理器的数量以及任务的 CPU 密集程度.Java并发实践提出了以下公式来估计最佳线程数:
The optimal number of threads to use depends on several factors, but mostly the number of available processors and how cpu-intensive your tasks are. Java Concurrency in Practice proposes the following formal formula to estimate the optimal number of threads:
N_threads = N_cpu * U_cpu * (1 + W / C)
地点:
- N_threads 是最佳线程数
- N_cpu 是处理器的数量,可以从
Runtime.getRuntime().availableProcessors();
- U_cpu 是目标 CPU 利用率(如果要使用全部可用资源,则为 1)
- W/C 是等待时间与计算时间的比率(0 表示受 CPU 限制的任务,可能 10 或 100 表示慢速 I/O 任务)
因此,例如,在 CPU 密集型场景中,您将拥有与 CPU 一样多的线程(有人主张使用该数字 + 1,但我从未见过它产生显着差异).
So for example, in a CPU-bound scenario, you would have as many threads as CPU (some advocate to use that number + 1 but I have never seen that it made a significant difference).
对于慢速 I/O 过程,例如网络爬虫,如果下载页面比处理页面慢 10 倍,则 W/C 可能为 10,在这种情况下,使用 100 个线程会很有用.
For a slow I/O process, for example a web crawler, W/C could be 10 if downloading a page is 10 times slower than processing it, in which case using 100 threads would be useful.
但是请注意,在实践中存在一个上限(使用 10,000 个线程通常不会加快速度,并且您可能会遇到 OutOfMemoryError,然后才能使用正常的内存设置启动它们).
Note however that there is an upper bound in practice (using 10,000 threads will generally not speed things up, and you would probably get an OutOfMemoryError before you can start them all anyway with normal memory settings).
如果您对应用程序运行的环境一无所知,这可能是您所能得到的最佳估计.在生产中分析您的应用程序可能使您能够微调设置.
This is probably the best estimate you can get if you don't know anything about the environment in which your application runs. Profiling your application in production might enable you to fine tune the settings.
虽然不是严格相关,但您可能也对 阿姆达尔定律感兴趣,该定律旨在在测量并行化程序时可以预期的最大加速.
Although not strictly related, you might also be interested in Amdahl's law, which aims at measuring the maximum speed-up you can expect from parallelising a program.
这篇关于基于编号的线程配置.CPU 核数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!