The SingleThreadExecutor in Java is a part of the java.util.concurrent
package and provides a way to create a thread pool that consists of only one thread. It ensures that tasks are executed sequentially (one after another) in the order they are submitted. If additional tasks are submitted while a task is running, they are placed in a queue and executed once the current task completes.
Key Features of SingleThreadExecutor:
- Sequential Execution: Only one task is executed at a time. If multiple tasks are submitted, they are executed sequentially in the order they are received.
- Single Thread: A single worker thread is created to handle the execution of all submitted tasks.
- Thread Reuse: The same thread is reused to execute tasks unless it terminates due to failure or shutdown.
- Thread Safety: It automatically manages the synchronization, so tasks don’t need to handle concurrent execution themselves.
Creating a SingleThreadExecutor:
You can create a SingleThreadExecutor
using the factory method Executors.newSingleThreadExecutor()
.
Example of SingleThreadExecutor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SingleThreadExecutorExample { public static void main(String[] args) { // Create a SingleThreadExecutor ExecutorService executor = Executors.newSingleThreadExecutor(); // Submit tasks to the executor for (int i = 1; i <= 5; i++) { final int taskNumber = i; executor.submit(() -> { System.out.println("Executing Task " + taskNumber + " by " + Thread.currentThread().getName()); try { Thread.sleep(1000); // Simulate some work with a 1 second delay } catch (InterruptedException e) { e.printStackTrace(); } }); } // Shutdown the executor executor.shutdown(); } } |
Output Example:
1 2 3 4 5 |
Executing Task 1 by pool-1-thread-1 Executing Task 2 by pool-1-thread-1 Executing Task 3 by pool-1-thread-1 Executing Task 4 by pool-1-thread-1 Executing Task 5 by pool-1-thread-1 |
In this example:
- All tasks are executed by the same thread (
pool-1-thread-1
). - Tasks are executed in the order they are submitted, one after another.
Key Methods of SingleThreadExecutor:
submit(Runnable task)
: Submits a task for execution and returns aFuture
representing the pending result.shutdown()
: Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.shutdownNow()
: Attempts to stop all actively executing tasks and halts the processing of waiting tasks.awaitTermination()
: Blocks until all tasks have completed execution after a shutdown request.
When to Use SingleThreadExecutor:
- Task Sequencing: When you need tasks to execute in a strictly defined sequence.
- Thread Reuse: When you want to reuse a single thread for multiple tasks to avoid creating multiple threads.
- Prevent Concurrency Issues: Since tasks are executed sequentially, you can avoid concurrency issues such as race conditions.
Important Notes:
- Thread Termination: If the thread terminates unexpectedly (due to an unhandled exception), a new thread will be created to continue processing remaining tasks.
- Blocking Tasks: If one task is blocked (e.g., waiting on I/O), subsequent tasks will be delayed until the current task finishes.
In summary, SingleThreadExecutor
is a great way to execute tasks in a serialized and controlled manner using a single background thread. It simplifies task management and helps ensure that tasks are executed one after another without worrying about thread synchronization issues.