- 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
How to schedule tasks in Java to run for repeated fixed-rate execution, beginning after the specified delay
One of the methods of the Timer class is void scheduleAtFixedRate(TimerTask task, long delay, long period). This method schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
In fixed-rate execution, each execution is scheduled with respect to the scheduled run time of the initial execution. Fixed-rate execution is apt for repetitive activities that are respond to absolute time. Likewise,fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain in sync.
Declaration − The java.util.Time.scheduleAtFixedRate(TimerTask task, long delay, long period) method is declared as follows −
public void scheduleAtFixedRate(TimerTask task, long delay, long period)
Here, task is the task to be scheduled, delay is the delay in milliseconds after which the task is executed and period is the time in milliseconds between successive task executions.
There are few exceptions thrown by the scheduleAtFixedRate(Timertask task, long delay, long period) method. They are as follows −
IllegalArgumentException | This exception is thrown if delay is negative, or delay + System.currentTimeMillis() is negative or period is <=0 |
IllegalStateException | This exception is thrown if task was scheduled or cancelled beforehand, 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-rate execution, beginning after the specified delay −
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,300,2000); // scheduling the task after the delay at fixed-rate } }
Output
Task is running Task is running Task is running Task is running Task is running
- Related Articles
- How to schedule tasks in Java to run for repeated fixed-delay execution, beginning after the specified delay
- 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 at the specified time
- Schedule a task for repeated fixed delay execution in Java
- Schedule a task for execution in Java after a given delay
- Schedule a task for execution in Java
- How to set delay for MySQL trigger/procedure execution?
- How to schedule background tasks (jobs) in ASP.NET Core?
- How to run multiple async tasks and waiting for them all to complete in C#?
- Program to schedule tasks to take smallest amount of time in Python
- How to call a method after a delay?
- How to call a function after a delay in Kotlin?
- How to measure execution time for a Java method?
- Is it possible to resume java execution after exception occurs?
- How to call a method after a delay in Swift(iOS)?
