Java CachedThreadPool vs FixedThreadPool Java CachedThreadPool vs FixedThreadPool multithreading multithreading

Java CachedThreadPool vs FixedThreadPool


The most important difference between a cached thread pool and a fixed thread pool in Java is that the cached thread pool has no upper limit on the number of threads it will spawn and use. Which one is preferred depends on what you want the scaling behavior to be like.

The main advantage of the cached thread pool is that threads will begin execution immediately even if you have an unanticipated large number of tasks to execute. For example, your business needs may increase over months or years, and your application may be moved to more powerful machines, and use of a cached thread pool will permit servicing the increased needs by using the increased available processing power without having to change the code. This can be an advantage since once the application has been in service for months or years, people may not remember the code well enough easily to identify the thread limit as a parameter that can be changed to improve performance.

The main advantage of the fixed thread pool is that the number of threads is more tightly controlled. This helps prevent other parts of the software installation - either within the application or in other applications - from being starved of processing power should this application receive a large number of tasks in a short period of time. In addition, the risk of running into the operating system threading limit is reduced, reducing the risks of software crashes that can result when a process needs to spawn a thread and is not able to do so.