Difference between Fixed thread pool and cached thread pool.

JavaServer Side ProgrammingProgramming

Executor framework are designed using thread pool concept. Thread pool is the way to reuse the already created thread instead of creating a new thread every time to execute the current task.

Executors class provides a factory method to create thread pools. The ThreadPoolExecutor class is the base implementation for the executors that are returned from many of the Executors  factory methods.

Sr. No.KeyFixed Thread PoolCached Thread Pool
1
Basic
As per Java Doc −
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.
As per Java Doc −
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.
2
Queue
It uses Blocking Queue.


It uses SynchronousQueue
queue .


3
Thread Lifetime
It  will keep all the threads running until they are explicitly terminated
Threads that have not been used for sixty seconds are terminated and removed from the cache
4.
Thread Pool Size
The thread pool size is fixed so it won’t grow.
The thread pool can grow from zero threads to Integer.MAX_VALUE
5.
Use Case
We should used fixedthreadpool, when we wanted to limit the concurrent task
It can be used when you have lots of predictable tasks.
 

Example of Fixed thread pool

public class Main {
   public static void main(String args[]) throws InterruptedException {
      ExecutorService executors = Executors.newFixedThreadPool(4);
      CountDownLatch latch= new CountDownLatch(2);
      executors.submit(new Service1(latch));
      executors.submit(new Service2(latch));
      latch.await();
      System.out.println("Done");
   }

import java.util.concurrent.CountDownLatch;
public class Service1 implements Runnable {
   CountDownLatch latch;
   public Service1(CountDownLatch latch) {
      super();
      this.latch = latch;
   }
   @Override
   public void run() {
      try {
         Thread.sleep(20000);
      } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         latch.countDown();
         System.out.println("Services2"+latch.getCount());
      }
   }
   import java.util.concurrent.CountDownLatch;
   public class Service2 implements Runnable {
      CountDownLatch latch;
      public Service2(CountDownLatch latch) {
         super();
         this.latch = latch;
      }
      @Override
      public void run() {
         try {
            Thread.sleep(20000);
         } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   latch.countDown();
   System.out.println("Services2"+latch.getCount());
   }
}
raja
Published on 09-Sep-2020 13:10:23
Advertisements