VOOZH about

URL: https://www.geeksforgeeks.org/java/java-util-concurrent-executorservice-interface-with-examples/

⇱ Java.util.concurrent.ExecutorService Interface with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java.util.concurrent.ExecutorService Interface with Examples

Last Updated : 13 May, 2022

The ExecutorService interface extends Executor by adding methods that help manage and control the execution of threads. It is defined in java.util.concurrent package. It defines methods that execute the threads that return results, a set of threads that determine the shutdown status. The ExecutorService interface is implemented in a utility class called Executors. It defines methods that provide an implementation of the ExecutorService interface and many other interfaces, with some default settings.

The class hierarchy is as follows: 

--> java.util.concurrent Package
 --> Interface ExecutorService Class 

Note: ScheduledExecutorService is Implementing Sub-Interfaces and classes implemented are as follows:

  • AbstractExecutorService
  • ForkJoinPool
  • ScheduledThreadPoolExecutor
  • ThreadPoolExecutor

Methods in Executor Interface

MethodAction Performed
awaitTermination()Waits for all the tasks to complete their execution after a shutdown request is found. It waits for the time specified by the timelimit argument
invokeAll()Executes all the tasks contained in the collection. The list of Future objects is returned which contains the status and return values of the various tasks
 invokeAny()Executes all the tasks contained in the collection. On completion of any single task it returns its result and all the other tasks are canceled.
isShutdown()Tells whether the invoking executor is shut down or not. Returns true if shutdown otherwise returns false
isTerminated()Checks if all the tasks have been completed post-shutdown. Return true if completed, otherwise returns false.
shutdown()Causes all the currently executing tasks to terminate after completion in the order in which they were started and rejects any new incoming tasks.
shutdownNow()Forcefully terminates all the tasks, regardless of their current state i.e running, waiting, or ready. The lists of tasks were in a ready state in return.
submit()Adds a task that returns a result to the list of executing tasks for execution. It returns a Future object which returns the result of the task after completion

Implementation: Executors 

Output:

👁 Image
 
Comment