设置线程池的理想大小 [英] Setting Ideal size of Thread Pool

查看:238
本文介绍了设置线程池的理想大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-



newSingleThreadExecutor与newFixedThreadPool(20)有什么区别?

$ b $



每当我使用 newSingleThreadExecutor 运行我的程序时, code>我的程序工作得很好,端到端延迟(第95百分位数)在 5ms



但是一旦我开始运行我的程序使用 -



newFixedThreadPool(20)



我的程序性能下降,我开始看到端到端延迟 37ms



所以现在我试图从架构的角度来理解这里的线程数是什么意思?



如果我使用更多的线程数,那么会发生什么?



如果任何人可以用一个外行人的语言向我解释这些简单的事情,那么这对我是非常有用的。感谢您的帮助。



我的机器配置规格 - 从Linux机器运行我的程序 -

 处理器:0 
vendor_id:GenuineIntel
cpu系列:6
型号:45
型号名称:Intel(R)Xeon -2670 0 @ 2.60GHz
steps:7
cpu MHz:2599.999
缓存大小:20480 KB
fpu:yes
fpu_exception:yes
cpuid level :13
wp:yes
flags:fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm arat pln pts
bogomips:5199.99
clflush size:64
cache_alignment:64
地址大小:40位物理,48位虚拟
电源管理:

处理器:1
vendor_id:GenuineIntel
cpu系列:6
型号:45
型号名称:Intel (R)Xeon CPU E5-2670 0 @ 2.60GHz
步进:7
cpu MHz:2599.999
缓存大小:20480 KB
fpu:是
fpu_exception:yes
cpuid level:13
wp:yes
标志:fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm arat pln pts
bogomips:5199.99
clflush size:64
cache_alignment:64
地址大小:40位物理,48位虚拟
电源管理:


解决方案

好的。理想情况下,假设您的线程没有锁定,使得它们不会彼此阻塞(彼此独立),并且您可以假定工作负载(处理)相同,则结果是池大小 Runtime.getRuntime()。availableProcessors() availableProcessors()+ 1 给出最好的结果。 b

但是说,如果线程相互干扰或I / O进入,那么Amadhal的定律很好地解释。从wiki,


Amdahl定律指出,如果P是可以并行化的程序的比例(即受益于并行化)和(1-P)是不能并行化的比例(保持串行),则使用N个处理器可以实现的最大加速比是




在您的情况,基于可用内核的数量,以及他们正在做什么工作(纯计算?I / O?持有锁?某些资源被阻塞等),您需要根据上述参数提出解决方案。



例如:几个月后,我参与从数字网站收集数据。我的机器是4核,我有一个池大小 4 。但是因为操作纯粹是 I / O ,我的净速度很体面,我意识到我最好的性能与池大小 7 。这是因为,线程不是为了计算能力,而是为了I / O。所以我可以利用这样的事实,更多的线程可以正面竞争核心。



PS:我建议,通过本章性能从Java - Goetz。它详细处理这些事项。


What is the difference between-

newSingleThreadExecutor vs newFixedThreadPool(20)

in terms of Operating System and Programming point of view.

Whenever I am running my program using newSingleThreadExecutor my program works very well and end to end latency(95th percentile) comes around 5ms.

But as soon as I start running my program using-

newFixedThreadPool(20)

my program performance degrades and I start seeing end to end latency as 37ms.

So now I am trying to understand from architecture point of view what does number of threads means here? And how to decide what is the optimal number of threads I should choose?

And if I am using more number of threads then what will happen?

If anyone can explain me these simple things in a layman language then that will be very useful to me. Thanks for the help.

My machine config spec- I am running my program from Linux machine-

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
stepping        : 7
cpu MHz         : 2599.999
cache size      : 20480 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm arat pln pts
bogomips        : 5199.99
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 45
model name      : Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz
stepping        : 7
cpu MHz         : 2599.999
cache size      : 20480 KB
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good xtopology tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm arat pln pts
bogomips        : 5199.99
clflush size    : 64
cache_alignment : 64
address sizes   : 40 bits physical, 48 bits virtual
power management:

解决方案

Ok. Ideally assuming your threads do not have locking such that they do not block each other (independent of each other) and you can assume that the work load (processing) is same, then it turns out that, have a pool size of Runtime.getRuntime().availableProcessors() or availableProcessors() + 1 gives the best results.

But say, if threads interfere with each other or have I/O inlvolved, then Amadhal's law explains pretty well. From wiki,

Amdahl's law states that if P is the proportion of a program that can be made parallel (i.e., benefit from parallelization), and (1 − P) is the proportion that cannot be parallelized (remains serial), then the maximum speedup that can be achieved by using N processors is

In your case, based upon the number of cores available, and what work they precisely do (pure computation? I/O? hold locks? blocked for some resource? etc..), you need to come up with the solution based upon above parameters.

For example: Some months back I was involved with collecting data from numeral web-sites. My machine was 4-core and I had a pool size of 4. But because the operation was purely I/O and my net speed was decent, I realized that I had best performance with a pool size of 7. And that is because, the threads were not fighting for computational power, but for I/O. So I could leverage the fact that more threads can contest for core positively.

PS: I suggest, going through the chapter Performance from the book - Java Concurrency in Practice by Brian Goetz. It deals with such matters in detail.

这篇关于设置线程池的理想大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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