Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question
3  
I bet you won't like Erlang. –  SK-logic Feb 23 '12 at 15:55
2  
It depends on where you are bound. More threads than cores is not really all that bad. –  Rig Feb 23 '12 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. –  David Lively Feb 23 '12 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 Feb 23 '12 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... –  Rig Feb 23 '12 at 16:39
show 7 more comments

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

share|improve this answer
1  
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 Feb 28 '12 at 8:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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