Java newCachedThreadPool Example

1. Introduction

Java’s Executors.newCachedThreadPool() factory method provides a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. Our goal is to understand how to use this thread pool to execute asynchronous tasks efficiently.

2. What is newCachedThreadPool?

The newCachedThreadPool method from the java.util.concurrent.Executors class is a factory method that creates a unbounded thread pool. It sets maximum pool size to Integer.Max. This thread pool can dynamically adjust to the workload by creating new threads as needed and reusing idle threads. Threads that have not been used for 60 seconds are terminated and removed from the cache, making this thread pool ideal for executing short-lived asynchronous tasks.

3. Executors.newCachedThreadPool() Example

Let’s create a Task. Here Task will be to read different files and process them.

Let’s create a ThreadPoolExecutor that will consume the task mentioned above and process it.

When you run above program, you will get below output:

If you notice, we have submitted 10 tasks, and it has created 10 new threads depending on demand. If any thread remains idle for more than one minute, it will be torn down. The newCachedThreadPool is a good choice when you want better queuing performance than what is offered by newFixedThreadPool. If you want to restrict the number of concurrent tasks for resource management, go with newFixedThreadPool.

Here is flow for the program:
  1. The program starts and initializes the thread pool using Executors.newCachedThreadPool().
  2. For each file from 1 to 10, a new FetchDataFromFile task is created and submitted to the thread pool.
  3. The thread pool assigns threads to execute these tasks. Each task simulates reading a file by sleeping for 5 seconds.
  4. As tasks are executed, messages are printed to indicate the start and completion of file reading.
  5. After all tasks have been submitted, the thread pool is shut down.

4. Difference between newFixedThreadPool() and newCacheThreadPool() Factory Methods

  • Thread Count: newFixedThreadPool maintains a fixed number of threads, while newCachedThreadPool creates threads as needed and removes them when they’re idle.
  • Queueing Behavior: newFixedThreadPool queues tasks when all threads are busy, leading to potential memory issues with a large number of tasks. newCachedThreadPool minimizes queuing by creating new threads as needed.
  • Use Cases: newFixedThreadPool is suitable for applications with a known, limited concurrency level, whereas newCachedThreadPool is better for handling a large number of short-lived tasks efficiently.

5. Conclusion

The newCachedThreadPool method offers a highly efficient way to handle concurrent tasks, especially when dealing with numerous short-lived asynchronous tasks. It outperforms the simple thread creation approach by reusing threads, which reduces the overhead and increases performance. Utilizing newCachedThreadPool in Java applications can lead to more efficient and scalable concurrency management.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *