[Threads - Java] What's the max number of threads I should use?

1) Start Approach

The short and quick answer is to have # threads = CPU/Cores. Why have a thread if there's nothing to run it? Therefore, one thread per process/core will maximize processing power and minimize context switching. This assumption is a good starting approach.

public static final int THREADS =  Runtime.getRuntime().availableProcessors();

2) Detailed Approach: CPU-bound or I/O bound?

But the more correct answer is - it depends if your task is CPU-bound or I/O-bound?

2.1) I/O-bound

If your thread has to wait for network packet or disk block, then CPU time is wasted waiting. In this case, you will want to consider having more threads per process, as your program can work on another thread instead of just waiting.
However, there is an overhead of adding threads and additional work that will get accomplished.

2.2) CPU-bound

But if your thread is CPU heavy, then a 1:1 correlation makes more sense, because adding more threads will only slow it down. 

3) Conclusion

It depends. You should benchmark the application's performance.
Also, this assumes that multi-threaded code is written properly. If the original developer only had one CPU, then he would never run into performance issues when benchmarking.
Another tip is to consider allowing # of threads to be configured at run time rather than compile time.

Resource

Comments

Popular posts from this blog

[Redis] Redis Cluster vs Redis Sentinel

[Unit Testing] Test Doubles (Stubs, Mocks....etc)

[Node.js] Pending HTTP requests lead to unresponsive nodeJS