Java – sets the ideal size of the thread pool
See English answers > performance issues with newfixedthreadpool vs newsinglethreadexecutior1
newSingleThreadExecutor vs newFixedThreadPool(20)
Views on operating system and programming
Whenever I run my program using the newsinglethreadexecutor, my program works well, with an end-to-end delay (95th percentile) of about 5ms
But once I start running my program,
Newfixedthreadpool (20)
The performance of my program is declining, and I began to see an end-to-end delay of 37 Ms
Now I'm trying to understand what this thread means from the perspective of architecture? And how do I decide what is the best number of threads I should choose?
What happens if I use more threads?
It's very useful for me if someone can explain these simple things to me in layman's language Thank you for your help
My machine configuration specification – I run my program from a 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:
Solution
OK Ideally, if your threads are not locked so that they will not block each other (independent of each other), and you can assume that the workload (processing) is the same, it turns out that its pool size is runtime getRuntime (). Availableprocessors () or availableprocessors () 1 gives the best result
But if threads interfere with each other or have I / O, amadhal's law is good From wiki,
In your case, according to the number of cores available and the work they do accurately (pure calculation? I / O? Keep locking some resources? Etc.), you need to according to the solution parameters proposed above
For example, a few months later, I participated in collecting data from digital websites My machine is 4-core. I have a swimming pool with a size of 4 But because the operation is pure I / O, my net speed is decent, and I realize that I have the best performance with a pool size of 7.5 That's because threads are not used for computing power, but for I / O. so I can take advantage of the fact that more threads can actively participate in the core competition
PS: I suggest that the performance in this book - Brian Goetz's Java concurrency practice It deals with these things in detail