Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java CachedThreadPool vs FixedThreadPool

I ran load test on my application to find maximum number of threads needed to support the planned load.

I have used ExecutorService CachedThreadPool so that threads are created dynamically based on load. Now I know the value of max thread count using getLargestPoolSize().

Should I replace CachedThreadPool with FixedThreadPool(maxValue), to avoid the large (Integer.MAX_VALUE) thread creation behaviour of CachedThreadPool?

Suggest pros and cons of each of them.

like image 799
MIK Avatar asked Sep 25 '15 22:09

MIK


People also ask

What is difference between Cachedthreadpool and fixed thread pool?

3. Fixed Thread Pool. As opposed to the cached thread pool, this one is using an unbounded queue with a fixed number of never-expiring threads. Therefore, instead of an ever-increasing number of threads, the fixed thread pool tries to execute incoming tasks with a fixed amount of threads.

Is ForkJoinPool introduced in Java 7 always a better alternative to ThreadPoolExecutor?

1) The main difference between ForkJoinPool and ThreadPoolExecutor is that ForkJoinPool is designed to accept and execute ForkJoinTask, which is a lightweight version of FutureTask, while ThreadPoolExecutor is designed to provide a normal thread pool which executes each submitted task using one of possibly several ...

What is difference between newFixedThreadPool and newCachedThreadPool?

In terms of resources, the newFixedThreadPool will keep all the threads running until they are explicitly terminated. In the newCachedThreadPool Threads that have not been used for sixty seconds are terminated and removed from the cache. Given this, the resource consumption will depend very much in the situation.

Which type of thread pool will use previously?

newCachedThreadPool. Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks.


1 Answers

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.

like image 149
Warren Dew Avatar answered Nov 08 '22 05:11

Warren Dew