Java – a reasonable number of threads for a Java program

Architecturejavamultithreading

In Java (or probably most other languages targeting the JVM) what is a reasonable number of threads to use? Presumably, this will be expressed as a ratio to the number of cores/processors available, is that correct?

At least initially, it seems like twice as many threads as cores might make sense, but 20+ times as many seems unreasonable. Is the ratio likely to be affected by the type and/or architecture of the software? Are there types of software and/or situations in which a much larger number of threads really makes sense?

[And yes, I know that's phrased as a number of separate questions, but I think they're closely enough related for a single answer to cover all of them, since they're all really about how many threads make sense under what circumstances.]

Best Answer

Lots of comments, but no answer, yet. Here's a quick stab...

I would say that it depends on what your threads are doing. You can have hundreds of threads, but if the vast majority of them are just waiting, they won't incur much overhead. On the other hand, if you are doing computation, I would limit the number of threads to the number of cores. You won't get any extra pure computation speed from scheduling more. In addition, in these cases, you may want to look into task parallelism as an alternative.

If your code is a mix of waiting and processing, the answer is less clear. Aside from various strategies you can take to speed things up, your best bet in this case is to do profiling to find an optimal number.