- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Schedule a task for repeated fixed delay execution in Java
In fixed-delay execution, each execution is scheduled with respect to the original execution time of the preceding execution. If an execution is delayed for a particular reason (case in point, garbage collection), the subsequent executions will be delayed as well.
There are two ways in which a task can be scheduled for repeated fixed-delay execution. They are as follows −
Scheduling a task for repeated fixed-delay execution at a specified time
Scheduling a task for repeated fixed-delay execution after a specified delay
Scheduling a task for repeated fixed-delay execution at a specified time
The void schedule(TimerTask task, Date firstTime, long period) method schedules tasks for repeated fixed-delay execution, beginning at the specified time.
Declaration −The java.util.Timer.schedule(TimerTask task, Date firstTime, long period) is declared as follows −
public void schedule(TimerTask task, Date firstTime, long period)
Here, the task is the task to be scheduled, the first time is the first time at which the task is executed and the period is the time in milliseconds between successive task executions.
There are few exceptions thrown by the schedule(Timertask task, Date firstTime, long period) method. They are as follows −
IllegalArgumentException | This exception is thrown if firstTime.getTime is negative or period is <=0 |
IllegalStateException | This exception is thrown if the task was scheduled or cancelled beforehand, the timer was cancelled, or timer thread terminated. |
NullPointerException | This exception is thrown if the task is null. |
Let us see an example showing how to schedule tasks in Java to run for repeated fixed-delay execution, beginning at the specified time −
Example
import java.util.*; class MyTask extends TimerTask { public void run() { System.out.println("Task is running"); } } public class Example { public static void main(String[] args) { Timer timer = new Timer(); // creating timer TimerTask task = new MyTask(); // creating timer task timer.scheduleAtFixedRate(task,new Date(),2000); // scheduling the task at the specified time at fixed-delay } }
Output
Task is running Task is running Task is running Task is running Task is running
Scheduling a task for repeated fixed-delay execution after a specified delay
The void schedule(TimerTask task, long delay, long period) method schedules tasks for repeated fixed-delay execution, beginning after the specified delay.
Declaration −The java.util.Timer.schedule(TimerTask task, long delay, long period) is declared as follows −
public void schedule(TimerTask task, long delay, long period)
Here, the task is the task to be scheduled, the delay is the delay in milliseconds after which the task is executed and the period is the time in milliseconds between successive task executions.
Here, the task is the task to be scheduled, the first time is the first time at which the task is executed and the period is the time in milliseconds between successive task executions.
There are few exceptions thrown by the schedule(Timertask task, long delay, long period) method. They are as follows −
IllegalArgumentException | This exception is thrown if firstTime.getTime is negative or period is <=0 |
IllegalStateException | This exception is thrown if the task was scheduled or cancelled beforehand, the timer was cancelled, or timer thread terminated. |
NullPointerException | This exception is thrown if the task is null. |
Let us see a program illustrating the use of the void schedule(TimerTask task, long delay, long period) method −
Example
import java.util.*; class MyTask extends TimerTask { public void run() { System.out.println("Task is running"); } } public class Example { public static void main(String[] args) { Timer timer = new Timer(); // creating timer TimerTask task = new MyTask(); // creating timer task // scheduling the task for repeated fixed-delay execution, beginning after the specified delay timer.schedule(task, 800, 1000); } }
Output
Task is running Task is running Task is running Task is running Task is running Task is running Task is running Task is running Task is running Task is running
- Related Articles
- Schedule a task for execution in Java after a given delay
- How to schedule tasks in Java to run for repeated fixed-delay execution, beginning after the specified delay
- Schedule a task for execution in Java
- How to schedule tasks in Java to run for repeated fixed-delay execution, beginning at the specified time
- How to schedule tasks in Java to run for repeated fixed-rate execution, beginning after the specified delay
- How to schedule tasks in Java to run for repeated fixed-rate execution, beginning at the specified time
- How to set delay for MySQL trigger/procedure execution?
- How to execute a task repeatedly after fixed time intervals in iOS
- How to execute Async task repeatedly after fixed time intervals in Android?
- How to measure execution time for a Java method?
- Cancel the Timer Task in Java
- How to execute an Async task repeatedly after fixed time intervals in Android using Kotlin?
- Set a delay for the CSS transition effect
- Get execution stats in MongoDB for a collection
- What is fixed for floating and fixed to fixed swaps?
