Is having a single threadpool better design than multiple threadpools

The purpose of having separate dedicated threadpools is so that an activity doesn’t get starved for threads because other activities took all the threads. If some service has its own threadpool then it is assured of having a certain number of threads at its disposal and it’s not as sensitive to demands made by other services.

OS Threads are a limited resource. If you have an application that uses threads for different purposes, some of them might become busy and keep a lot of threads working for them, or some service might have a bug where in some circumstance a thread isn’t returned to the pool. If that can happen to one thread, the same circumstance may be applicable to all of the threads, and a whole thread pool can be drained this way. (There is an example early on in the Release It! book describing a situation where a database was being switched over, and badly-written JDBC code caused a leak like this.)

With the multiple dedicated threadpools if a service needs too many threads then it has to wait for threads to be available, introducing back-pressure into the system so that it degrades gradually, and since other parts have their own thread pools they have a chance to catch their parts up. So the idea is that the system should have more stable characteristics as load changes. In the case you describe having a separate threadpool for scheduled tasks makes sure that those tasks get run regardless of how busy the rest of the system is.

The multiple threadpools would require tuning to make sure each pool had enough threads and not too many. With a single threadpool then that might reduce the number of idle threads and might make better use of more threads, but you would not have the predictability of knowing some important task would get the threads it needed to finish in a timely manner.

Leave a Comment