简体   繁体   English

在决定线程池大小时如何看待超线程?

[英]How to regard Hyper-Threading when deciding thread pool size?

I've read several Questions and articles regarding how to decide on a size for a thread pool. 我已经阅读了几个关于如何决定线程池大小的问题和文章。 Questions include: 问题包括:

Articles include: 文章包括:

However, none of these directly address the issue of Hyper-Threading on Intel chips. 但是,这些都没有直接解决英特尔芯片上的超线程问题。

On a computer with Hyper-Threading enabled, should I consider the virtual cores when deciding thread pool size? 在启用了超线程的计算机上,在决定线程池大小时是否应该考虑虚拟内核?

Take for example Brian Goetz' advice in the Java Concurrency In Practice book that, generally speaking, for a CPU-bound app one might consider using ( # of cores + 1 ) as the number of threads. 以Brian Goetz在Java Concurrency In Practice一书中的建议为例,一般来说,对于一个CPU绑定的应用程序,可以考虑使用( # of cores + 1 )作为线程数。 For an Intel Core i7 chip with 4 real cores and 8 virtual (Hyper-Threading) cores, would that formula be ( 4 + 1 ) or ( 8 + 1 ) ? 对于具有4个真实内核和8个虚拟(超线程)内核的英特尔酷睿i7芯片,该公式是( 4 + 1 )还是( 8 + 1 )

Also, how much of difference does the nature of the app matter in how to consider the Hyper-Threading cores? 此外,应用程序的性质在如何考虑超线程核心方面有多大差异?

Contrary to the mention above, my own app is not CPU bound. 与上面提到的相反,我自己的应用程序不受CPU限制。 Instead my app is a server-side Vaadin app where the threads are making Internet connections and hitting a local database through JDBC, several times a minute. 相反,我的应用程序是服务器端Vaadin应用程序,其中线程正在建立Internet连接并通过JDBC访问本地数据库,每分钟几次。 Given that Hyper-Threading is basically a second set of registers attached to the same core, perhaps a CPU-bound app should consider only real cores while a network/IO-bound app should consider virtual cores? 鉴于超线程基本上是连接到同一核心的第二组寄存器,也许一个受CPU限制的应用程序应该只考虑真正的核心,而网络/ IO绑定的应用程序应该考虑虚拟核心?

Lastly, does the type of Intel chip affect Hyper-Threading and therefore the calculation of thread pool size? 最后, 英特尔芯片的类型是否会影响超线程 ,从而影响线程池大小? Specifically, is there a difference regarding this issue between a Xeon and a Core i7/i5 ? 具体来说, XeonCore i7 / i5之间的这个问题是否存在差异? For example, the difference between a current MacBook (Core i7) and Mac Pro (Xeon). 例如,当前MacBook(Core i7)和Mac Pro(Xeon)之间的差异。

I realize there are many variables involved, and this is a complex topic. 我意识到涉及许多变量,这是一个复杂的主题。 No perfectly precise answers are possible. 没有完全精确的答案是可能的。 I'm just looking for some general rules and advice to help out programmers like me who are not as savvy about such hardware matters. 我只是在寻找一些通用的规则和建议,以帮助像我这样对这些硬件问题不了解的程序员。

How to regard Hyper-Threading when deciding thread pool size? 在决定线程池大小时如何看待超线程?

The short answer is Don't. 简短的回答是不要。

The longer answer is that Goetz's "formula" is actually just a rule of thumb. 更长的答案是,Goetz的“公式”实际上只是一个经验法则。 The language used here 这里使用的语言

... "generally speaking, for a CPU-bound app one might consider using ( # of cores + 1 ) as the number of threads" ...... “一般来说,对于CPU绑定的应用程序, 可以考虑使用(核心数量+ 1)作为线程数量”

makes that clear. 说清楚。 There are all sorts of reasons why that "rule of thumb" number might give you suboptimal performance. 为什么“经验法则”数字可能会给你次优的性能有各种各样的原因。

The correct approach is: 正确的方法是:

  1. Pick a number 选一个号码
  2. Measure performance 衡量表现
  3. Adjust number and go to step 2. 调整数字并转到步骤2。

.... until you arrive at a thread pool size that gives roughly the best answer for your use-case. ....直到您到达线程池大小,为您的用例提供大致最佳答案。

The other thing to note is that when you are building a server-based system is that performance is only one of many considerations. 另外需要注意的是,当您构建基于服务器的系统时,性能只是众多考虑因素之一。 Another is how your system performs under extreme load. 另一个是您的系统在极端负载下的性能。 If you optimize your performance based on a "best case" workload without considering behaviour under overload situations, then you can be in for a nasty shock if something goes wrong. 如果您根据“最佳情况”工作负载优化性能而不考虑过载情况下的行为,那么如果出现问题,您可能会遇到令人讨厌的冲击。

Optimizing solely for maximum throughput can have bad consequences... 针对最大吞吐量进行优化会产生不良后果......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM