ScheduledExecutorService Interface in Java

19 Oct, 2020
The ScheduledExecutorService interface in Java is a sub-interface of ExecutorService interface defined in java.util.concurrent package. This interface is used to run the given tasks periodically or once after a given delay. The ScheduledExecutorService interface has declared some useful methods to schedule the given tasks. These methods are implemented by the ScheduledThreadPoolExecutor class.
Declaration
public interface ScheduledExecutorService extends ExecutorService

The Hierarchy of ScheduledExecutorService
Implementing class
The implementing class of ScheduledExecutorService is ScheduledThreadPoolExecutor.
Creating a ScheduledExecutorService object 
Since ScheduledExecutorService is an interface, so it can not be instantiated. But the Executors class, defined in java.util.concurrent package, provides some factory methods that return ScheduledExecutorService objects( objects of its implementing classes )
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) : Creates a new scheduled thread pool with a given core pool size (corePoolSize) and returns a ScheduledExecutorService object which can be downcasted to ScheduledThreadPoolExecutor object. This object can be used to run tasks after a given delay or to execute periodically. 
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize , ThreadFactory threadFactory) : Creates a new scheduled thread pool with a given core pool size (corePoolSize) and returns a ScheduledExecutorService object which can be downcasted to ScheduledThreadPoolExecutor object. The second argument is a ThreadFactory object that is used when a new thread is created.
Example of ScheduledExecutorService interface : 
Java
// Java Program to demonstrate// SchedulerExecutorService  import java.util.concurrent.*;import java.util.*;import java.io.*;  class SchedulerExecutorServiceExample {        public static void main(String[] args)    {        System.out.println(            "A count-down-clock program that counts from 10 to 0");          // creating a ScheduledExecutorService object        ScheduledExecutorService scheduler            = Executors.newScheduledThreadPool(11);          // printing the current time        System.out.println(            "Current time : "            + Calendar.getInstance().get(Calendar.SECOND));          // Scheduling the tasks        for (int i = 10; i >= 0; i--) {            scheduler.schedule(new Task(i), 10 - i,                               TimeUnit.SECONDS);        }          // remember to shutdown the scheduler        // so that it no longer accepts          // any new tasks        scheduler.shutdown();    }}  class Task implements Runnable {    private int num;    public Task(int num) { this.num = num; }    public void run()    {        System.out.println(            "Number " + num + " Current time : "            + Calendar.getInstance().get(Calendar.SECOND));    }}
Output:
A count-down-clock program that counts from 10 to 0
Current time : 28
Number 10 Current time : 28
Number 9 Current time : 29
Number 8 Current time : 30
Number 7 Current time : 31
Number 6 Current time : 32
Number 5 Current time : 33
Number 4 Current time : 34
Number 3 Current time : 35
Number 2 Current time : 36
Number 1 Current time : 37
Number 0 Current time : 38

This is a Countdown Clock that counts from 10 to 0. The ScheduledExexutorService object i.e; scheduler is created using the Executors.newScheduledThreadPool(int corePoolSize) method. 
Note: All the tasks executed after (10 – i) seconds delay from the invocation of schedule() method. The value of the current time may vary in the output based on the time of execution.
METHOD
DESCRIPTION
METHOD
DESCRIPTION
METHOD
DESCRIPTION
Java-concurrent-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.