Java Executors.newSingleThreadExecutor Example
May 25, 2022
On this page we will learn using Executors.newSingleThreadExecutor
method.
1. The
newSingleThreadExecutor
creates an Executor
that uses a single worker thread operating off an unbounded queue.
2. If the single thread created by
newSingleThreadExecutor
method, terminates because of any failure before shutdown then a new thread will take its place if needed to execute subsequent tasks.
3. Tasks are guaranteed to execute sequentially. At any given time only one task will be active.
4. To create thread, we can use default thread factory as well as given thread factory.
a. Method with default thread factory.
static ExecutorService newSingleThreadExecutor()
Executor
.
b. Method with given thread factory.
static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory)
Executor
.
Now find the examples to use
newSingleThreadExecutor()
method.
Example-1
Find thenewSingleThreadExecutor()
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 task = () -> { int count = 0; for (; count < 5; count++) { System.out.println(count); } }; ExecutorService execService = Executors.newSingleThreadExecutor(); execService.submit(task); execService.awaitTermination(5, TimeUnit.SECONDS); execService.shutdownNow(); } }
0 1 2 3 4
Example-2
Find thenewSingleThreadExecutor()
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<Integer> task = () -> { int count = 0; for (; count < 5; count++) { System.out.println(count); } return count; }; ExecutorService execService = Executors.newSingleThreadExecutor(); Future<Integer> future = execService.submit(task); int output = future.get(); System.out.println("Total count:" + output); execService.awaitTermination(5, TimeUnit.SECONDS); execService.shutdownNow(); } }
0 1 2 3 4 Total count:5
Example-3
Find thenewSingleThreadExecutor()
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 task = () -> { int count = 0; for (; count < 5; count++) { System.out.println(count); } }; ThreadFactory threadFactory = new MaxPriorityThreadFactory(); ExecutorService execService = Executors.newSingleThreadExecutor(threadFactory); execService.submit(task); execService.awaitTermination(5, 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; } }
0 1 2 3 4
newSingleThreadExecutor() vs newFixedThreadPool(1)
SimilarityThe
newSingleThreadExecutor()
returns ExecutorService
with single thread worker and newFixedThreadPool(1)
also returns ExecutorService
with single thread worker. In both cases if thread terminates, new thread will be created.
Difference
The
ExecutorService
returned by newSingleThreadExecutor()
, can never increase its thread pool size whereas the ExecutorService
returned by newFixedThreadPool(1)
may reconfigure to use additional threads.