简体   繁体   中英

A common executorservice(fixed thread pool) for complete web application or a new thread pool using executorService for each and every request?

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?

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 (eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM