Table of Contents
In this tutorial, we will learn about Executor’s newSingleThreadExecutor factory method.
In the last tutorial, I have shared an introduction to ThreadPoolExecutor. If you are not aware of concepts of ThreadPoolExecutor, you should go through that first.
Executor’s newSingleThreadExecutor factory method :
This method returns thread pool executor which executes one task at a time.If you have submitted n task to executors, it will execute it one by one.If this thread gets interrupted then a new thread will be created for executing the tasks.
Syntax:
1 2 3 |
ExecutorService executorService=Executors.newSingleThreadExecutor(); |
Java newSingleThreadExecutor example:
Let’s create a very simple example.
Step 1: Create a Runnable task named "LoopTask.java".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.java2blog; public class LoopTask implements Runnable { private String loopTaskName; public LoopTask(String loopTaskName) { super(); this.loopTaskName = loopTaskName; } @Override public void run() { System.out.println("Starting "+loopTaskName); for (int i = 1; i <= 10; i++) { System.out.println("Executing "+loopTaskName+" with "+Thread.currentThread().getName()+"===="+i); } System.out.println("Ending "+loopTaskName); } } |
Step 2: Create a class named "SingleThreadPoolMain". This will be our main class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SingleThreadPoolMain { public static void main(String args[]) { ExecutorService es=Executors.newSingleThreadExecutor(); for (int i = 1; i <= 3; i++) { LoopTask loopTask=new LoopTask("LoopTask "+i); es.submit(loopTask); } es.shutdown(); } } |
Let’s run above program to check the output:
Executing LoopTask 1 with pool-1-thread-1====1
Executing LoopTask 1 with pool-1-thread-1====2
Executing LoopTask 1 with pool-1-thread-1====3
Executing LoopTask 1 with pool-1-thread-1====4
Executing LoopTask 1 with pool-1-thread-1====5
Executing LoopTask 1 with pool-1-thread-1====6
Executing LoopTask 1 with pool-1-thread-1====7
Executing LoopTask 1 with pool-1-thread-1====8
Executing LoopTask 1 with pool-1-thread-1====9
Executing LoopTask 1 with pool-1-thread-1====10
Ending LoopTask 1
Starting LoopTask 2
Executing LoopTask 2 with pool-1-thread-1====1
Executing LoopTask 2 with pool-1-thread-1====2
Executing LoopTask 2 with pool-1-thread-1====3
Executing LoopTask 2 with pool-1-thread-1====4
Executing LoopTask 2 with pool-1-thread-1====5
Executing LoopTask 2 with pool-1-thread-1====6
Executing LoopTask 2 with pool-1-thread-1====7
Executing LoopTask 2 with pool-1-thread-1====8
Executing LoopTask 2 with pool-1-thread-1====9
Executing LoopTask 2 with pool-1-thread-1====10
Ending LoopTask 2
Starting LoopTask 3
Executing LoopTask 3 with pool-1-thread-1====1
Executing LoopTask 3 with pool-1-thread-1====2
Executing LoopTask 3 with pool-1-thread-1====3
Executing LoopTask 3 with pool-1-thread-1====4
Executing LoopTask 3 with pool-1-thread-1====5
Executing LoopTask 3 with pool-1-thread-1====6
Executing LoopTask 3 with pool-1-thread-1====7
Executing LoopTask 3 with pool-1-thread-1====8
Executing LoopTask 3 with pool-1-thread-1====9
Executing LoopTask 3 with pool-1-thread-1====10
Ending LoopTask 3
We have used new newSingleThreadExecutor, so when we have submitted 3 tasks, 1 new thread will be created and will execute 1 task at time. Other 2 tasks will wait in wait queue. As soon as one task will be completed by thread, another task will be picked by this thread and will execute it.
Difference between newSingleThreadExecutor and newFixedThreadPool(1):
If you might notice there is quite similarity between newSingleThreadExecutor and newFixedThreadPool(1).
You can not change thread pool size of executors returned by newSingleThreadExecutor but you can change thread pool size of executors returned by newFixedThreadPool(1) by calling setCorePoolSize() of the class ThreadPoolExecutor.
That’s all about Java newSingleThreadExecutor example.
You may also like: