Java.util.concurrent.ExecutorService Interface with Examples

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
Implementation: Executors 
Java
// Java Program to Demonstrate ExecutorService Interface // Importing required classesimport java.util.concurrent.*; // Class// Main classpublic 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 classclass 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:
 
surindertarika1234
anikakapoor
solankimayank
sumitgumber28
Java - util package
Java-concurrent-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Stack Class in Java
Singleton Class in Java
Set in Java
Introduction to Java
Constructors in Java