Javatpoint Logo
Javatpoint Logo

Java ExecutorService

The Java ExecutorService is the interface which allows us to execute tasks on threads asynchronously. The Java ExecutorService interface is present in the java.util.concurrent package. The ExecutorService helps in maintaining a pool of threads and assigns them tasks. It also provides the facility to queue up tasks until there is a free thread available if the number of tasks is more than the threads available.

Java ExecutorService

Methods of Java ExecutorService

Method Description
boolean awaitTermination(long timeout, TimeUnit unit) This method blocks the task to enter ExecutorService until all the tasks have completed after the shutdown request, or the given timeout occurs, or the current thread is interrupted, whichever happens first.
<T> List invokeAll(Collection<? extends Callable<T>> tasks) This method executes the list of tasks given and returns the list of Futures which contain the results of all the tasks when completed.
<T> List invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) This method executes the list of tasks given and returns the list of Futures which contain the results of all the tasks when completed or the timeout expires, whichever occurs first.
<T> T invokeAny(Collection<? extends Callable<T>> tasks) This method executes the list of tasks given and returns the result of one task which gets completed without throwing any exception.
<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout,TimeUnit unit) This method executes the list of tasks given and returns the result of one task which gets completed without throwing any exception before the timeout elapses.
boolean isShutdown() This method returns whether the given executor is shut down or not.
boolean isTerminated() This method returns true if all tasks have executed after shutdown.
void shutdown() This method allows completion of previously submitted tasks to the ExecutorService and doesn?t allow any other tasks to be accepted.
List shutdownNow() This method stops all the actively executing tasks, stops the execution of queued up tasks, and returns the list of tasks which are queued up.
<T> Future<T> submit(Callable<T> task) This method submits a value-returning task for execution and returns the Future, which represents the pending result of the task.
Future<?> submit(Runnable task) This method submits a task for execution and returns a Future representing that task. It returns null upon successful completion.
<T> Future<T> submit(Runnable task, T result) This method submits a task for execution and returns a Future representing that task.

A simple program of Java ExecutorService

Output:

Java ExecutorService

In this program, we are creating an ExecutorService with ten threads and assigning it an anonymous runnable implementation which performs a task to print "ExecutorService" and after its task is over, we are shutting down the executor service.

How to use Java ExecutorService

Instantiating ExecutorService

We can use Java ExecutorService to create a single thread, a pool of threads, or a scheduled pool of threads. The Executors class provides factory methods to instantiate an ExecutorService as follows-

Assigning tasks to ExecutorServices

To assign a task to ExecutorService, we can use the following methods-

  • execute(Runnable task)
  • submit(Runnable task) / submit(Callable<T> task)
  • invokeAny(Collection<? extends Callable<T>> tasks)
  • invokeAll(Collection<? extends Callable<T>> tasks)

Example of assigning a task to ExecutorService using execute() method

The Java ExecutorService's execute() method takes in a runnable object and performs its task asynchronously. After making the call to execute method, we call the shutdown method, which blocks any other task to queue up in the executorService.

Output:

ExecutorService

Example of assigning a task to ExecutorService using submit()

The submit() method takes in a runnable object and returns a Future object. This object is later on used to check the status of Runnable whether it has completed execution or not.

Example of assigning a task to ExecutorService using invokeAny() method

The invokeAny() method takes a collection of Callablle objects or objects of classes implementing Callable. This method returns the future object of the callable object which gets successfully executed first.

Output:

result = Task 1

The result stores Task 1 as the first callable object is successfully executed first.

Example of assigning a task to ExecutorService using invokeAll() method

The invokeAll() method takes in a Collection of Callable objects having tasks and returns a list of Future objects containing the result of all the tasks.

Output:

future.get = Task 1
future.get = Task 3
future.get = Task 2

How to shut down ExecutorService

Once we are done with our tasks given to ExecutorService, then we have to shut it down because ExecutorService performs the task on different threads. If we don't shut down the ExecutorService, the threads will keep running, and the JVM won?t shut down.

The process of shutting down can be done by the following three methods-

  • shutdown() method
  • shutdownNow() method
  • awaitTermination() method

Next TopicJava Tutorial




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA