6

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.]

12
  • 4
    I bet you won't like Erlang.
    – SK-logic
    Commented Feb 23, 2012 at 15:55
  • 2
    It depends on where you are bound. More threads than cores is not really all that bad.
    – rlperez
    Commented Feb 23, 2012 at 15:55
  • @Rig I can see that with, say, ~8 threads on quad-core machine, but with 20 or a 100, task switching overhead becomes significant.
    – 3Dave
    Commented Feb 23, 2012 at 15:56
  • 1
    Hi David, all questions here are expected to be asked in a fair and impartial tone: ranting is not what we do here. If you can rephrase your question to something that's specifically answerable that doesn't doesn't contain a rant, feel free to do so and flag your question for moderator review.
    – user8
    Commented Feb 23, 2012 at 16:36
  • 2
    So you are saying most of the complex software we use is wrong? Looking at Firefox on my mac with 2 tabs open I see it is running 26 threads...42 if you include the flash plugin. Chrome 33. Even my terminal is running 5...
    – rlperez
    Commented Feb 23, 2012 at 16:39

1 Answer 1

5

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.

1
  • 2
    Actually I would want about a 2:1 ratio or even 3:1 between threads and cores, so that when something blocks something else can be swapped in. In Erlang generally you would want something like a 100:1 or more, but Erlang uses a much lighter weight process model
    – Zachary K
    Commented Feb 28, 2012 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.