In Java, CachedThreadPool
and FixedThreadPool
are two implementations of the ExecutorService
interface that can be used to manage a pool of threads for executing tasks concurrently.
CachedThreadPool
creates new threads on demand and reuses idle threads when available. It is suitable for short-lived tasks that do not block, but it can quickly create a large number of threads if the task load is high. The threads are terminated after being idle for 60 seconds by default, which can help to avoid resource exhaustion.
On the other hand, FixedThreadPool
creates a fixed number of threads when it is created and reuses them until the service is terminated. It is suitable for longer-running tasks or when a fixed number of threads is required. The number of threads in the pool is fixed and cannot be increased dynamically.
Here’s an example that demonstrates the use of both thread pools:
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolExample { public static void main(String[] args) { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5); // Submit tasks to the cached thread pool for (int i = 0; i < 10; i++) { cachedThreadPool.submit(new Task("CachedThreadPool Task " + i)); } // Submit tasks to the fixed thread pool for (int i = 0; i < 10; i++) { fixedThreadPool.submit(new Task("FixedThreadPool Task " + i)); } // Shutdown the thread pools when done cachedThreadPool.shutdown(); fixedThreadPool.shutdown(); } } class Task implements Runnable { private String name; public Task(String name) { this.name = name; } public void run() { System.out.println("Executing " + name + " on " + Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Completed " + name + " on " + Thread.currentThread().getName()); } }
In this example, we create both CachedThreadPool
and FixedThreadPool
instances using the Executors
utility class. We then submit tasks to both thread pools using the submit()
method. Each task simply prints a message indicating that it is being executed and then sleeps for one second before completing.
Note that we need to shut down the thread pools when we are done with them using the shutdown()
method to ensure that any remaining tasks are executed and the threads are terminated properly.