1

In a java web project, users send their tasks to the web. To handle these tasks, I use a thread pool that contains 10 threads to handle their tasks. Every task will be built to a thread and send to the thread pool. The thread pool is designed to a static variable, which means that all of the tasks(threads) share the same thread pool.
Today, 200 users come to my web and then, they complained that their tasks ran very very slow...

  • are The threads in the static thread pool is too few? Maybe the number should be 30? 50 ?
  • Maybe I shouldn't use a static thread pool?
  • Maybe I shouldn't use a thread pool, since there are lots of tasks, I can just taskThread.start() ?
zaihui
  • 53
  • 1
  • 9

1 Answers1

1

Usually, you would figure out how IO bound your processing is and the amount of cores, you have available. The perfect formulae would be:

number_of_cores*(1/cpu_sticky)

Where cpu_sticky is between 0 and 1. So if you perceive it to be highly IO, cpu_sticky is very low (say .1), whereas a highly CPU intensive have a high cpu_sticky (say .9).

Most likely though is that code requires long DB searches, or searches, that lock one or more rows in a DB, so one or more threads needs to wait for completion. In that case have many threads, but each is still slow.

Niels Bech Nielsen
  • 4,777
  • 1
  • 21
  • 44
  • As a side note for finding number_of_cores in java one can use it like that; int number_of_cores = Runtime.getRuntime().availableProcessors(); – mrgenco Oct 31 '17 at 13:14