Java newSingleThreadExecutor example

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:

Java newSingleThreadExecutor example:

Let’s create a very simple example.

Step 1: Create a Runnable task named "LoopTask.java".

Step 2: Create a class named "SingleThreadPoolMain". This will be our main class.

Let’s run above program to check the output:

Starting LoopTask 1
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:

Error: View 12d15fexqk may not exist

import_contacts

You may also like:

Related Posts

  • Difference between Runnable and Callable in java
    16 December

    Difference between Runnable and Callable in java

    Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread. Difference between Runnable and Callable interface in java Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable. Runnable […]

  • 13 June

    Java Executor framework tutorial with example

    Table of ContentsExecutorExecutorServiceScheduledExecutorServiceExecutorsThreadPoolExecutor Java 5 has introduced new framework called Executor Framework for managing threads.We have already seen before how to create a thread. If you have noted, we need to create an object of thread class using new Thread(runnableObject), so we need to create thread object for each task.Imagine a situation where you have […]

  • Java ExecutorCompletionService
    09 June

    Java ExecutorCompletionService

    Table of ContentsWhy you may need to use ExecutorCompletionService:Java ExecutorCompletionService example: In this post, we will see about Java ExecutorCompletionService example. ExecutorCompletionService class implements CompletionService. This class returns Future object in completion order. Why you may need to use ExecutorCompletionService: Let’s understand with the help of scenario: Let’s say you have 5 tasks, you submit it to the […]

  • 29 May

    Java ScheduledThreadPoolExecutor Example

    Table of ContentsJava ScheduledThreadPoolExecutor Example:ScheduledThreadPoolExecutor’s scheduleAtFixedRate example:ScheduledThreadPoolExecutor’s scheduleWithFixedDelay example: There are multiple ways to schedule a task in java. We have already Java timer to schedule a task but the problem with timers task is that you can execute one task at a time.So if the current task takes longer subsequent job will be delayed. […]

  • Java FutureTask example
    28 May

    Java FutureTask example

    Table of ContentsJava FutureTask example:Explanation: In this tutorial, we will see about Java FutureTask example. FutureTask class has been introduced in JDK 5 with Executor Framework. FutureTask class is the concrete implementation of the Future object and provides methods for start and cancel the task.It also provides method to see if the computation is done […]

  • 20 May

    Java newFixedThreadPool example

    Table of ContentsSyntax:Java newFixedThreadPool example: In this tutorial, we will learn about Executor’s newFixedThreadPool 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 newFixedThreadPool factory method : This method returns ThreadPoolExecutor whose maximum size(let’s say […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.