-
Notifications
You must be signed in to change notification settings - Fork 13
Java newScheduledThreadPool Method Example
In this tutorial, we will learn about the Executor’s newScheduledThreadPool factory method.
Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
newScheduledThreadPool Method returns ScheduledExecutorService interface
ScheduledExecutorService interface provides The schedule() method to create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate() and scheduleWithFixedDelay() methods create and execute tasks that run periodically until cancelled.
Commands submitted using the Executor.execute(Runnable) and ExecutorService submit methods are scheduled with a requested delay of zero. Zero and negative delays (but not periods) are also allowed in schedule methods, and are treated as requests for immediate execution.
Let's understand the methods of ScheduledExecutorService Interface and newScheduledThreadPool() factory method with an example.
public class SchedulingTasksWithScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main started");
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
// Create a task
Runnable task2 = () -> {
System.out.println("Executing the task2 at: " + new Date());
};
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
System.out.println("Scheduling task to run after 5 seconds... " + new Date());
scheduledExecutorService.schedule(task1, 5, TimeUnit.SECONDS);
scheduledExecutorService.schedule(task2, 5, TimeUnit.SECONDS);
scheduledExecutorService.shutdown();
System.out.println("Thread main finished");
}
}
Output:
Thread main started
Scheduling task to run after 5 seconds... Sat Sep 01 10:56:40 IST 2018
Thread main finished
Executing the task1 at: Sat Sep 01 10:56:45 IST 2018
Executing the task2 at: Sat Sep 01 10:56:45 IST 2018
scheduledExecutorService.schedule() function takes a Runnable, a delay value, and the unit of the delay. The above program executes the task after 5 seconds from the time of submission.
Now let’s see an example where we execute the task periodically -
public class SchedulingTasksWithScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main started");
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
scheduledExecutorService.scheduleAtFixedRate(task1, 0, 2, TimeUnit.SECONDS);
System.out.println("Thread main finished");
}
}
Output:
Thread main started
Thread main finished
Executing the task1 at: Sat Sep 01 11:03:16 IST 2018
Executing the task1 at: Sat Sep 01 11:03:18 IST 2018
Executing the task1 at: Sat Sep 01 11:03:20 IST 2018
Executing the task1 at: Sat Sep 01 11:03:22 IST 2018
Executing the task1 at: Sat Sep 01 11:03:24 IST 2018
......
Note that if the task encounters an exception, subsequent executions of the task are suppressed. Otherwise, the task will only terminate if you either shut down the executor or kill the program.