Java.util.concurrent.ExecutorService Interface with Examples Last Updated : 13 May, 2022 Comments Improve Suggest changes Like Article Like Report 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: AbstractExecutorServiceForkJoinPoolScheduledThreadPoolExecutorThreadPoolExecutorMethods in Executor InterfaceMethodAction PerformedawaitTermination()Waits for all the tasks to complete their execution after a shutdown request is found. It waits for the time specified by the timelimit argumentinvokeAll()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 falseisTerminated()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 Java // Java Program to Demonstrate ExecutorService Interface // Importing required classes import java.util.concurrent.*; // Class // Main class public class SimpleExecutor { // Main driver method public static void main(String[] args) { // Creating objects of CountDownLatch class CountDownLatch cd1 = new CountDownLatch(5); CountDownLatch cd2 = new CountDownLatch(5); CountDownLatch cd3 = new CountDownLatch(5); CountDownLatch cd4 = new CountDownLatch(5); // Creating objects of ExecutorService class ExecutorService es = Executors.newFixedThreadPool(2); // Display message only for better readability System.out.println("Starting"); // Executing the tasks es.execute(new MyThread(cd1, "A")); es.execute(new MyThread(cd2, "B")); es.execute(new MyThread(cd3, "C")); es.execute(new MyThread(cd4, "D")); // Try block to check for exceptions try { // Waiting for tasks to complete cd1.await(); cd2.await(); cd3.await(); cd4.await(); } // Catch block to handle exceptions catch (InterruptedException e) { System.out.println(e); } // Making all current executing threads to terminate es.shutdown(); // Display message only for better readability System.out.println("Done"); } } // Class 2 // Helper class class MyThread implements Runnable { // Class data members String name; CountDownLatch latch; // Constructor MyThread(CountDownLatch latch, String name) { // this keyword refers to current instance itself this.name = name; this.latch = latch; new Thread(this); } // Method // Called automatically when thread is started public void run() { for (int i = 0; i < 5; i++) { System.out.println(name + ": " + i); latch.countDown(); } } } Output: Comment More info C CharchitKapoor Follow Improve Article Tags : Java Java - util package Java-concurrent-package Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like