Difference between Fixed and Cached Thread pool in Java Executor Famework

There are mainly two types of thread pools provided by Javas' Executor framework, one is fixed thread pool, which starts with fixed number of threads and other is cached thread pool which creates more threads if initial number of thread is not able to do the job. The newCachedThreadPool() method is used to create a cached pool, while newFixedThreadPool() is used to construct a thread of fixed size. Cached thread pool executes each task as soon as they are submitted, using an existing thread if its idle or creating new threads otherwise, while in case of fixed thread pool, if more tasks are submitted then idle threads then those task are put into a queue and later executed once any other task has finished.

Difference between fixed and cached thread pool in Java

Here are key difference between a fixed and cached thread pools in Java. 

1. When you submit a task they are immediately executed in cached thread but they can be placed in a queue, if no thread is free in case of fixed thread pool. This means, if you want guaranteed and immediate execution then using a cached thread pool is better option but if you are resource constraint and want to use fixed number of threads then fixed thread pool is a better choice. 

2. You can use Executors.newCachedThreadPool() factory method to create a cached pool in Java and Executors.newFixedThreadPool() to create fixed thread pool.  Bot methods are part of Java Concurrency API and defined in Executors utility class. 

3. Cached thread pool can create more threads if required, but no extra threads are created in fixed thread pool. This means cached thread pool is a better choice if you have resources as it can create and increase number of threads in the pool if load increases but fixed thread pool is a better choice in case of resource constraint environment. 

Example of Fixed Thread Pool

Here is an example of Fixed thread pool in Java:

fixed thread pool vs cached thread pool in Java

Example of Cached Thread Pool

Here is an example of cached thread pool in Java:

Difference between fixed and cached thread pool in Java

That's all about how to use fixed and cached thread pools in Java. Both of these thread pools are popular ones and if you have used Executor framework in Java then you have definitely used one of them. In this article, we have learned the key differences between both of them and also learned their strength and weaknesses and how they work. 

If you want to remember the difference then just remember their name, a fixed thread pools means number of threads in the pool is fixed while in case of cached thread pool, number of threads can increase or decrease depending upon load. 

Other Java Multithreading and Concurrency Articles you may like
  • 10 Java Multithreading and Concurrency Best Practices (article)
  • The Complete Java Developer RoadMap (roadmap)
  • Difference between CyclicBarrier and CountDownLatch in Java? (answer)
  • 10 Courses to learn Java in for Beginners (courses)
  • How to avoid deadlock in Java? (answer)
  • Understanding the flow of data and code in Java program (answer)
  • Is Java Concurrency in Practice still valid (answer)
  • 5 Courses to Learn Java Multithreading in-depth (courses)
  • Top 50 Multithreading and Concurrency Questions in Java (questions)
  • How to do inter-thread communication in Java using wait-notify? (answer)
  • 10 Tips to become a better Java Developer (tips)
  • Understanding the flow of data and code in Java program (answer)
  • 5 Essential Skills to Crack Java Interviews (skills)
  • How does Exchanger works in Java Multithreading (tutorial)
  • Top 5 Books to Master Concurrency in Java (books)

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P.S. - If you are new into Java Multithreading and Concurrency and need a free online training course to learn Multithreading and Concurrency basics then I also suggest you check out this Java Multithreading a free course on Udemy. It's completely free and all you need is a free Udemy account to join this course. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.