基于no的线程配置。 CPU核心 [英] Threads configuration based on no. of CPU-cores

查看:122
本文介绍了基于no的线程配置。 CPU核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我有一个示例应用程序,我有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.

Edited1:
任何人都可以建议任何有关如何为特定h / w配置设置基线的读数。

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 Concurrency in Practice 提出了以下形式公式来估算最佳线程数:

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是prcessors的数量,您可以从 Runtime.getRuntime()获取.availableProcessors();

  • U_cpu是目标CPU利用率(如果要使用完整的可用资源,则为1)

  • W / C是等待时间与计算时间的比率(对于CPU绑定任务为0,可能为10或100)缓慢的I / O任务)

  • N_threads is the optimal number of threads
  • N_cpu is the number of prcessors, which you can obtain from Runtime.getRuntime().availableProcessors();
  • U_cpu is the target CPU utilization (1 if you want to use the full available resources)
  • W / C is the ratio of wait time to compute time (0 for CPU-bound task, maybe 10 or 100 for slow I/O tasks)

因此,例如,在受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流程,例如网络爬虫, W / C c如果下载页面比处理页面慢10倍,那么应该是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.

虽然不是严格相关,但您可能也对 Amdahl定律,旨在衡量程序并行化可以带来的最大加速。

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.

这篇关于基于no的线程配置。 CPU核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆