How to determine the optimal number of Java threads when using a hyper-threaded multicore processor
My processor is 8x2600 MHz (Intel Xeon CPU E5-2670 0 @ 2.60 GHz).
I have a data processing algorithm that can be executed in parallel, written in Java. This function determines the number of parallel threads at runtime
Runtime.getRuntime().availableProcessors()
, which returns 8.
This algorithm does not block 100%. The CPU supports hyperthreading with two threads per core.
Now I have to run the algorithm with 8 threads, because Java only sees 8 cores, or should I use 16 Java threads, taking into account the hyper-threading provided by the processor?
source to share
The ideal number of threads depends on the task itself. Context switching on a modern processor can be somewhat expensive due to the fact that the data used in the computation is heavily cached. Consider a situation where there are no IO-related activities and no CPU cycles should be wasted. Then the maximum throughput will be reached at the expense of n_threads = n_cores. Context switching can be costly even if there is hyperthreading. But if there are I / O operations, the number of threads that exceed the number of cores can increase.
source to share