Fixed Thread pool has been used to limit the number of threads in java application by passing an integer variable to the executors method like below
Executors.newFixedThreadPool(10);
Suppose we are designing any application and we are using fixed thread pool then how can we take up a decision for an ideal fixed thread pool size or on what basis we should decide the fixed thread pool size ?
So the ideal thread pool size is 4 cores * 2 * ( 1 + 9 ) = 80. If all 100ms are calculation, 0ms is waiting. the blocking coefficent is 0. The ideal thread pool size is 4 * 2 * 1 = 8.
Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100. As requests come in, threads will be created up to 5 and then tasks will be added to the queue until it reaches 100. When the queue is full new threads will be created up to maxPoolSize .
The default configuration is a core pool size of 1, with unlimited max pool size and unlimited queue capacity.
First of all,
1.) Are you creating a FixedThreadPool
for the whole application or for specific task in your application?
2.) The server on which your application is deployed, supports how many CPU's, per CPU how many threads?
3.) There might be a case, where you are allocating fewer threads than the server has, or might allocate more, and your tasks doe not require that many. It is more or less a waste of resources.
Thread pool sizes should rarely be hard-coded; instead pool sizes should be provided by a configuration mechanism or computed dynamically by consulting Runtime.availableProcessors
.
For more, Java Concurrency in Practice has a detailed chapter on thread pools.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With