Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

executors.newcachedthreadpool() example
difference between newcachedthreadpool and newfixedthreadpool
executor vs executorservice
threadpoolexecutor vs executorservice
executors.newcachedthreadpool() size
threadpooltaskexecutor vs executorservice
newscheduledthreadpool vs newfixedthreadpool
executorservice java 8

newCachedThreadPool() versus newFixedThreadPool()

When should I use one or the other? Which strategy is better in terms of resource utilization?


I think the docs explain the difference and usage of these two functions pretty well:

newFixedThreadPool

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.

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. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.

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. For instance, If you have a huge number of long running tasks I would suggest the FixedThreadPool. As for the CachedThreadPool, the docs say that "These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks".

Executors newCachedThreadPool() vs newFixedThreadPool , Methods that create and return a ScheduledExecutorService set up with commonly useful public static ExecutorService newCachedThreadPool(). Creates a� Questions: newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource utilization? Answers: I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.


Just to complete the other answers, I would like to quote Effective Java, 2nd Edition, by Joshua Bloch, chapter 10, Item 68 :

"Choosing the executor service for a particular application can be tricky. If you’re writing a small program, or a lightly loaded server, using Executors.new- CachedThreadPool is generally a good choice, as it demands no configuration and generally "does the right thing." But a cached thread pool is not a good choice for a heavily loaded production server!

In a cached thread pool, submitted tasks are not queued but immediately handed off to a thread for execution. If no threads are available, a new one is created. If a server is so heavily loaded that all of its CPUs are fully utilized, and more tasks arrive, more threads will be created, which will only make matters worse.

Therefore, in a heavily loaded production server, you are much better off using Executors.newFixedThreadPool, which gives you a pool with a fixed number of threads, or using the ThreadPoolExecutor class directly, for maximum control."

Executors (Java Platform SE 8 ), of Executor Service, Callable, Future and Thread pool executor. A fixed thread pool can be created by calling newFixedThreadPool() of Executors class. is created by calling newCachedThreadPool() of Executors class. Executors.newCachedThreadPool versus Executors.newFixedThreadPool () Intereting Posts Création de services d’arrière-plan pour Bluetooth basse consommation sur Android Comment définir un boost :: optionnel retour à un état non initialisé?


If you look at the source code, you will see, they are calling ThreadPoolExecutor. internally and setting their properties. You can create your one to have a better control of your requirement.

public static ExecutorService newFixedThreadPool(int nThreads) {
   return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
}

Java Concurrency in a Nutshell : Types of Thread Pools (Part 3), public static ExecutorService newCachedThreadPool() {. return new ThreadPoolExecutor( 0 , // try to reduce the pool size to 0 if threads are idle� 1) separation allows you to balance the relative processing capacity of your request-processing workload vs scheduled tasks. In particular, if you use scheduled tasks to act as timeouts for incoming requests (e.g. if you are using Futures that get queued into your main thread pool), the separation can have a substantial impact on the timeliness


That’s right, Executors.newCachedThreadPool() isn't a great choice for server code that's servicing multiple clients and concurrent requests.

Why? There are basically two (related) problems with it:

  1. It's unbounded, which means that you're opening the door for anyone to cripple your JVM by simply injecting more work into the service (DoS attack). Threads consume a non-negligible amount of memory and also increase memory consumption based on their work-in-progress, so it's quite easy to topple a server this way (unless you have other circuit-breakers in place).

  2. The unbounded problem is exacerbated by the fact that the Executor is fronted by a SynchronousQueue which means there's a direct handoff between the task-giver and the thread pool. Each new task will create a new thread if all existing threads are busy. This is generally a bad strategy for server code. When the CPU gets saturated, existing tasks take longer to finish. Yet more tasks are being submitted and more threads created, so tasks take longer and longer to complete. When the CPU is saturated, more threads is definitely not what the server needs.

Here are my recommendations:

Use a fixed-size thread pool Executors.newFixedThreadPool or a ThreadPoolExecutor. with a set maximum number of threads;

Difference between CachedThreadPool vs FixedThreadPool , newCachedThreadPool() versus Executors.newFixedThreadPool(). newCachedThreadPool() versus newFixedThreadPool(). When should I use one or the� java.util.concurrent.ExecutorService java code examples, Learn how to use ExecutorService in java.util.concurrent, Get the top 19 code snippets using java.util.concurrent.ExecutorService


If you are not worried about an unbounded queue of Callable/Runnable tasks, you can use one of them. As suggested by bruno, I too prefer newFixedThreadPool to newCachedThreadPool over these two.

But ThreadPoolExecutor provides more flexible features compared to either newFixedThreadPool or newCachedThreadPool

ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, 
TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
RejectedExecutionHandler handler)

Advantages:

  1. You have full control of BlockingQueue size. It's not un-bounded, unlike the earlier two options. I won't get an out of memory error due to a huge pile-up of pending Callable/Runnable tasks when there is unexpected turbulence in the system.

  2. You can implement custom Rejection handling policy OR use one of the policies:

    1. In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.

    2. In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.

    3. In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.

    4. In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)

  3. You can implement a custom Thread factory for the below use cases:

    1. To set a more descriptive thread name
    2. To set thread daemon status
    3. To set thread priority

java, Simple Executor and ExecutorService Examples. 4. Cached Thread newCachedThreadPool(): creates an expandable thread pool executor. Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.)


Java Concurrency: Understanding Thread Pool and Executors, A cached thread pool can be obtainted by calling the static newCachedThreadPool() method of Executors class. Syntax. ExecutorService executor = Executors. Executors.newCachedThreadPool versus Executors.newFixedThreadPool Executors.newCachedThreadPool versus Executors.newFixedThreadPool () es mucho mejor utilizar


newCachedThreadPool Method, newCachedThreadPool() versus newFixedThreadPool() When should I use one or the other? Which strategy is better in terms of resource� That’s right, Executors.newCachedThreadPool() isn't a great choice for server code that's servicing multiple clients and concurrent requests. Why? There are basically two (related) problems with it: It's unbounded, which means that you're opening the door for anyone to cripple your JVM by simply injecting more work into the service (DoS attack).


Difference between newCachedThreadPool() and , By using the Future object returned by ExecutorService.submit() a thread pool of fixed number of threads and newCachedThreadPool()� Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable classes defined in this package. This class supports the following kinds of methods: