
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Fixed thread pool and cached thread pool.
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. | Key | Fixed Thread Pool | Cached 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()); } }
- Related Articles
- Difference between scheduledThread pool and Single Thread Executor.
- C# Program to Create a Thread Pool
- Check if a thread belongs to managed thread pool or not in C#
- Difference between Virtual Memory and Job Pool
- Difference between Process and Thread
- Difference between green and native thread
- Difference between Goroutine and Thread in Golang.
- Difference between Thread and Runnable in Java
- Difference between Types,Types,Type-POOL and TYPE-POOLS in SAP ABAP
- Difference Between Thread Class and Runnable Interface in Java
- Difference between the use of Type, Types and TYPE POOL in SAP ABAP programming.
- Main thread vs child thread in C#
- User Thread vs Daemon Thread in Java?
- What are the differences between yarn and thread?
- How a thread can interrupt another thread in Java?

Advertisements