Priyan Chandrapala
Priyan Chandrapala

Reputation: 83

How to determine the optimal number of java threads when using a hyperthreaded multi core CPU

My CPU is 8x2600 MHz (Intel Xeon CPU E5-2670 0 @ 2.60GHz).
I have a data processing algorithm which can run in parallel written in Java. This function determines the number of concurrent threads during runtime with Runtime.getRuntime().availableProcessors()which returns 8.

This algorithm is 100% non-blocking. The CPU supports hyper threading with 2 threads per core.

Now should I run the algorithm with 8 threads because Java sees only 8 cores, or should I use 16 Java threads taking in to consideration the hyper threading provided by the CPU?

Upvotes: 3

Views: 572

Answers (1)

Thusitha
Thusitha

Reputation: 155

The ideal number of threads depend on the task itself. Context switching in modern CPU's can be somewhat expensive due to the fact that data used in the computation is cached heavily. Consider the situation where there are no IO related activities and the CPU cycles need not be wasted waiting. Then the maximum throughput would be achieved by having n_threads = n_cores. Context switching might be expensive even when hyper threading is available. But if there are IO activities, increasing the thread count beyond the number of cores can benefit.

Upvotes: 1

Related Questions