0

I have an application which might use hundreds or even thousands of threads simultaneously, for a fixed period of time each.

What should be size of thread pool? - Is there a rule of thumb formula for this number. I understood using the number of processors is an advisable number. Is it the maximum number to set?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
dushkin
  • 1,939
  • 3
  • 37
  • 82
  • 1
    It really depends on the task and where the bottleneck is. If the process is CPU-bound, then the answer is "yes". If there's IO involved, the answer is "it depends". – Joe C Sep 09 '17 at 13:16
  • https://stackoverflow.com/questions/1980832/java-how-to-scale-threads-according-to-cpu-cores/36723383#36723383 – Ravindra babu Sep 10 '17 at 08:19

1 Answers1

1

For CPU bound tasks it should be number of processors.

int N = Runtime.getRuntime().availableProcessors();

For I/O bound tasks it can be more than number of processors , because some of threads may not be utilized during I/O intensive tasks. You should estimate ratio of wait time for your average I/O request. Once you get this ratio RT, you can use formula N*(1+RT), where N is number of processors.

fg78nc
  • 4,774
  • 3
  • 19
  • 32