1

To improve the response time of my request, I was planning to use executorService and divide my work among multiple threads and reduce the response time of the request.

But after reading articles and blogs creating a new thread pool for every request would also impact the performance.

What will be the idle approach, - 1. Should I create a new thread pool for each and every request? OR 2. Should I keep a fixed thread pool for the complete application and let each and every request use threads from that single thread pool?

Anmol Seth
  • 21
  • 6
  • See also [this related question](https://stackoverflow.com/questions/14458463/what-is-the-best-approach-to-run-a-long-process-from-a-java-servlet). – Andrew S Sep 05 '18 at 18:53
  • The reasonable options would seem to be 1) single threadpool for everything, and 2) dedicated threadpools for different kinds of tasks, see [Is having a single threadpool better design than multiple threadpools](https://stackoverflow.com/q/26243422/217324). I see no point to having a new threadpool for each and every request. – Nathan Hughes Sep 05 '18 at 19:12

1 Answers1

0

So, what is the point of creating a thread pool? Usually its either one or both of two reasons:

A) Your application can re-use threads instead of wasting lots of CPU time by destroying old threads and creating new ones to replace them.

B) It allows your application to control the number of threads it creates. Instead of creating some unlimited number of resource-hungry threads when a big burst of tasks (e.g., client requests) comes in, your application can instead create task objects in a nice, compact queue, and then it can use some reasonable number* of threads to process them

Creating thread pools means, creating and destroying the threads that those pools manage. So, if you created and destroyed a new thread pool for each client request, you would totally defeat both reasons (A) and (B).


* Preferably, determined by the number of CPUs, amount of memory, etc. that are available on the host.

Ohm's Lawman
  • 293
  • 1
  • 6