0

In a Java web application, I have a question about the ideal number of threads to create when using the newFixedThreadPool()

The examples from Java Concurrency in Practice always recommended creating it with Runtime.getRuntime().availableProcessors() + 1

However, in my web application, doing this seems to lock up the system for extended amounts of time (about 20-30 seconds), since there are also HTTP requests coming in from users and database threads for reading/writing data to the database.

Does anyone have a recommendation on the ideal number of threads to utilize towards the thread pool for crunching numbers, while still leaving some cores available for other aspects of running a responsive and speedy web application? Seems the example from the book is ideal in stand-alone applications, but not for web applications.

bluedevil2k
  • 9,366
  • 8
  • 43
  • 57
  • I suspect there is any correct answer for this? It will be a recommendation only and actual value can be best achieved by doing some bench-marking test on your environment. – sakura May 14 '14 at 14:09
  • 1
    `availableProcessors()` is a number that helps when sizing for computational intense tasks. Processing Web requests is quite the opposite— I/O intense tasks. You usually want to have far more threads in this scenario, read: when the CPU is idle most of the time. – Holger May 14 '14 at 16:27

1 Answers1

1

I think that solely depends on a particular application's architecture. There is just too many factors to consider to give a useful number.
But for instance Weblogic server, which one may consider "a generic web application", uses a self-tuning thread pool. It constantly checks performance stats and adjusts the thread pool (and other resources) accordingly.
So the thread number is not constant and always much more than a number of available cores. As Sakura mentioned you will have to load-test to find out.

Yuri
  • 1,695
  • 1
  • 13
  • 23