0% found this document useful (0 votes)
12 views

ExecutorService

Uploaded by

sharmilapv84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

ExecutorService

Uploaded by

sharmilapv84
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ExecutorService

The ExecutorService interface is part of the java.util.concurrent package and represents an


asynchronous task execution service. It extends the Executor interface, which defines a single
method execute(Runnable command) for executing tasks. It provides a higher-level replacement for the
traditional way of managing threads with Thread objects. It simplifies the management of thread
execution, especially when dealing with parallel tasks, and abstracts away much of the complexity
involved in manually creating and managing threads.

Key Features and Benefits of ExecutorService:

 Thread Pool Management: It provides thread pooling to manage a set of threads, which is more
efficient than creating a new thread for each task.
 Task Submission: It allows you to submit tasks that can be executed asynchronously.
 Graceful Shutdown: It supports graceful shutdown of threads, preventing new tasks from being
submitted but allowing already submitted tasks to finish.
 Asynchronous Execution: Allows tasks to be executed asynchronously and returns a Future object,
which can be used to retrieve the result of the task.
 Automatic Resource Management: Automatically manages threads, reusing threads to execute
tasks, improving system resource usage.

Core Methods of ExecutorService:

 submit(): Submits a task for execution and returns a Future object that can be used to retrieve the
result of the task.
 invokeAll(): Submits a collection of tasks for execution and waits for all tasks to complete.
 invokeAny(): Submits a collection of tasks for execution and returns the result of the first task that
successfully completes.
 shutdown(): Initiates an orderly shutdown of the executor, where previously submitted tasks are
executed, but no new tasks will be accepted.
 shutdownNow(): Attempts to stop all actively executing tasks, halts the processing of waiting tasks,
and returns a list of the tasks that were waiting to be executed.

Types of Executors in ExecutorService:

The ExecutorService interface has several implementations provided by the Executors utility class. Some
common ones are:

1) newFixedThreadPool(): Creates a thread pool with a fixed number of threads. It is a good choice
when you have a known number of concurrent tasks.

ExecutorService executor = Executors.newFixedThreadPool(4);

2) newCachedThreadPool(): Creates a thread pool that creates new threads as needed, but will reuse
previously constructed threads when they are available.

ExecutorService executor = Executors.newCachedThreadPool();

3) newSingleThreadExecutor(): Creates a single-threaded executor that executes tasks sequentially.

ExecutorService executor = Executors.newSingleThreadExecutor();

4) newScheduledThreadPool(): Creates a thread pool for scheduling tasks with fixed-rate or fixed-
delay execution policies.

ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);

Creation of Thread using ExecutorService

1. Using ExecutorService with Runnable interface: we'll submit Runnable tasks to an ExecutorService.
A Runnable task doesn't return a result but just performs some action.
2. Using ExecutorService with Callable interface: The Callable interface is similar to Runnable but it
can return a result and can throw exceptions. When using Callable, the tasks will return a Future
object, which allows you to retrieve the result once the task is complete.

Example of Using ExecutorService in Java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ExecutorServiceUsingCallable {


public static void main(String[] args) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(3); // Create a fixed thread pool with 3 threads
int i=20; int j=10;

Callable<Integer> task1 = () -> { // Create Callable tasks that return a result


int k = i + j;
return k;
};
Callable<Integer> task2 = () -> {
int a = i * j;
return a;
};
Callable<Integer> task3 = () -> {
int b = i - j;
return b;
};

// Submit tasks to the executor


Future<Integer> future1 = executor.submit(task1);
Future<Integer> future2 = executor.submit(task2);
Future<Integer> future3 = executor.submit(task3);

// Get the results from the tasks (blocking call)


System.out.println("Addition:::"+ future1.get()); // Prints result from task1
System.out.println("Multiplication:::" + future2.get()); // Prints result from task2
System.out.println("Subtraction:::" + future3.get()); // Prints result from task3

executor.shutdown(); // Shutdown the executor


}
}

Key Points:

 ExecutorService creates a thread pool, so tasks can be processed concurrently without creating
individual threads for each task.
 Future: The submit() method returns a Future object, which represents the result of an
asynchronous computation. You can call get() on a Future to block the main thread and retrieve the
result once the task completes.
 shutdown() and shutdownNow() are used to stop the executor and clean up resources.
shutdown() waits for tasks to finish, while shutdownNow() tries to stop all tasks immediately.

Use Cases for ExecutorService

 Parallel Task Execution: Useful when you need to execute multiple tasks concurrently, such as in
simulations or parallel data processing.
 Handling I/O-bound Tasks: For tasks that involve I/O operations (e.g., web scraping, file
processing), ExecutorService can handle multiple I/O tasks concurrently without blocking the main
thread.
 Managing Long-Running Tasks: Ideal for managing long-running tasks (e.g., in background
processes) and handling them without blocking the user interface or main thread.
 Asynchronous Execution: When you need to execute tasks asynchronously and potentially wait for
their results (using Future).
 Scheduled Task Execution: Scenario: Running tasks at fixed intervals, such as data synchronization
or periodic reporting. Example: Using ScheduledExecutorService to perform cleanup tasks every
hour.

What Are the Available Implementations of Executorservice in the Standard Library?


The ExecutorService interface has three standard implementations:

1) ThreadPoolExecutor — for executing tasks using a pool of threads. Once a thread is finished
executing the task, it goes back into the pool. If all threads in the pool are busy, then the task has to
wait for its turn.
2) ScheduledThreadPoolExecutor allows to schedule task execution instead of running it immediately
when a thread is available. It can also schedule tasks with fixed rate or fixed delay.
3) ForkJoinPool is a special ExecutorService for dealing with recursive algorithms tasks. If you use a
regular ThreadPoolExecutor for a recursive algorithm, you will quickly find all your threads are busy
waiting for the lower levels of recursion to finish. The ForkJoinPool implements the so-called work-
stealing algorithm that allows it to use available threads more efficiently.

What is the difference between execute () and submit ()?

 execute(): A method from the Executor interface; it does not return any result or future.
 submit(): Returns a Future object, allowing you to get results or exceptions.

How would you execute a task periodically using ExecutorService?


Use ScheduledExecutorService with methods like scheduleAtFixedRate() or scheduleWithFixedDelay().

What is the difference between shutdown() and shutdownNow()?

shutdown(): Gracefully stops accepting new tasks and allows ongoing tasks to complete.
shutdownNow(): Attempts to stop all ongoing tasks immediately and returns a list of pending tasks.

Explain the difference between Executor, ExecutorService, and ScheduledExecutorService.

Executor: A simple interface for task execution.


ExecutorService: An extension of Executor that adds methods for managing and controlling task
execution, including the ability to submit tasks and retrieve results.
ScheduledExecutorService: An interface for scheduling tasks to run after a given delay or periodically.

How do you handle exceptions thrown by tasks executed by the Executor Framework?

Exceptions thrown during task execution can be caught and handled within the run or call methods. If
using Future, you can retrieve the exception by calling get(), which will throw an ExecutionException if
the task failed.

What are some benefits of using a thread pool instead of creating new threads for every task?

Benefits include reduced overhead of thread creation, improved resource utilization, better control over
the maximum number of concurrent tasks, and reduced risk of resource exhaustion.

You might also like