Java Executors.newCachedThreadPool
May 31, 2022
On this page we will learn using Executors.newCachedThreadPool
method.
1. The
newCachedThreadPool
creates a thread pool that creates threads as needed. If the thread is already available, it will be reused.
2. The threads in this thread pool are short-lived. Threads that have not been used for sixty seconds are terminated and removed from the cache.
3. This thread pool is useful to execute many short-lived asynchronous tasks. These thread pools are used to improve the performance of programs.
4. This thread pool does not allow to remain a thread idle for a long time and so thread will not use any resource. Hence performance is increased.
The
newCachedThreadPool
has following method declarations.
a. With default thread factory.
static ExecutorService newCachedThreadPool()
static ExecutorService newCachedThreadPool(ThreadFactory threadFactory)
Now find the examples to use
newCachedThreadPool
.
Example-1
Find thenewCachedThreadPool
example with Runnable
.
ExecutorDemo1.java
package com.concretepage; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class ExecutorDemo1 { public static void main(final String... args) throws InterruptedException { Runnable task1 = () -> System.out.println("Executing Task-1 ..."); Runnable task2 = () -> System.out.println("Executing Task-2 ..."); ExecutorService execService = Executors.newCachedThreadPool(); execService.submit(task1); execService.submit(task2); execService.awaitTermination(3, TimeUnit.SECONDS); execService.shutdownNow(); } }
Executing Task-1 ... Executing Task-2 ...
Example-2
Find thenewCachedThreadPool
example with Callable
.
ExecutorDemo2.java
package com.concretepage; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; public class ExecutorDemo2 { public static void main(final String... args) throws InterruptedException, ExecutionException { Callable<String> task1 = () -> { System.out.println("Executing Task-1"); return "Task-1 Finish."; }; Callable<String> task2 = () -> { System.out.println("Executing Task-2"); return "Task-2 Finish."; }; ExecutorService execService = Executors.newCachedThreadPool(); Future<String> future1 = execService.submit(task1); Future<String> future2 = execService.submit(task2); String output1 = future1.get(); System.out.println(output1); String output2 = future2.get(); System.out.println(output2); execService.awaitTermination(3, TimeUnit.SECONDS); execService.shutdownNow(); } }
Executing Task-1 Executing Task-2 Task-1 Finish. Task-2 Finish.
Example-3
Find thenewCachedThreadPool
example with given ThreadFactory
.
ExecutorDemo3.java
package com.concretepage; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; public class ExecutorDemo3 { public static void main(final String... args) throws InterruptedException { Runnable task1 = () -> System.out.println("Executing Task-1 ..."); Runnable task2 = () -> System.out.println("Executing Task-2 ..."); ThreadFactory threadFactory = new MaxPriorityThreadFactory(); ExecutorService execService = Executors.newCachedThreadPool(threadFactory); execService.submit(task1); execService.submit(task2); execService.awaitTermination(3, TimeUnit.SECONDS); execService.shutdownNow(); } } class MaxPriorityThreadFactory implements ThreadFactory { @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setPriority(Thread.MAX_PRIORITY); return t; } }
Executing Task-1 ... Executing Task-2 ...