Question

  • I have a task which can I divide into multiple independent subtasks and Each subtask can be run in a separate thread in Java.
  • I want to understand what is the optimal number of threads after which increasing the number of threads will not lead to any performance gain.
Was it helpful?

Solution

There are a number of factors here. You are only limited by the number of processors/cores if your tasks are CPU bound. If your tasks tend to be IO bound, then you will likely benefit from more threads than processors/cores. How many depends on how much time your tasks are spending waiting on IO, relative to how much time they spend doing actual compututations.

So, as usual, it depends on what you're doing.

For CPU bound processes, the number of running threads shouldn't need to exceed the number of cores.

In the end, you will need to try different configurations and see what works best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top