Home » Java programming language

Java Timer scheduleAtFixedRate() Method with Example

Timer Class scheduleAtFixedRate() method: Here, we are going to learn about the scheduleAtFixedRate() method of Timer Class with its syntax and example.
Submitted by Preeti Jain, on March 26, 2020

Timer Class scheduleAtFixedRate() method

Syntax:

    public void scheduleAtFixedRate (TimerTask tt, Date ft, long period);
    public void scheduleAtFixedRate (TimerTask tt, long de, long period);
  • scheduleAtFixedRate() method is available in java.util package.
  • scheduleAtFixedRate (TimerTask tt, Date ft, long period) method is used to schedule the given task for constant rate execution repeatedly starting at the given time.
  • scheduleAtFixedRate (TimerTask tt, long delay, long period) method is used to schedule the given task for constant rate execution repeatedly starting after the given delay.
  • These methods may throw an exception at the time of the scheduling task.
    • IllegalArgumentException: This exception may throw when any one of the parameters is not in a range.
    • IllegalStateException: This exception may throw when the task was scheduled or canceled already.
  • These are non-static methods and it is accessible with the class object only and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, scheduleAtFixedRate (TimerTask tt, Date ft, long period)
    • TimerTask tt – represents the timer task to be scheduled.
    • Date ft – represents the timer task to be scheduled.
    • long period – represents the time in milliseconds between task executions.
  • In the first case, scheduleAtFixedRate (TimerTask tt, long de, long period)
    • TimerTask tt – represents the timer task to be scheduled.
    • long de – represents the first time at which the task is implemented.
    • long period – represents the time in milliseconds between task executions.

Return value:

In both the cases, the return type of the method is void, it returns nothing.

Example 1:

// Java program to demonstrate the example 
// of scheduleAtFixedRate() method of
// Timer

import java.util.*;

public class ScheduleAtFixedRateOfTimer {
 public static void main(String[] args) {
  // Instantaites a TimerTask and
  // Timer object
  TimerTask task = new ScheduleTask();
  Timer tmr = new Timer();

  System.out.println("tmr.scheduleAtFixedRate(task, new Date(), 1000): ");
  // By using scheduleAtFixedRate(task,date,period) method isto
  // schedule the task at a constant rate in a
  // repeated manner and starts at the given time 1000 ms
  tmr.scheduleAtFixedRate(task, new Date(), 1000);
 }
}

class ScheduleTask extends TimerTask {
 // Task defined in this method
 public void run() {
  System.out.println("Out Of Stock...Keep Working");
 }
}

Output

tmr.scheduleAtFixedRate(task, new Date(), 1000): 
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working

Example 2:

import java.util.*;

public class ScheduleAtFixedRateOfTimer {
 public static void main(String[] args) {
  // Instantaites a TimerTask and
  // Timer object
  TimerTask task = new ScheduleTask();
  Timer tmr = new Timer();

  System.out.println("tmr.scheduleAtFixedRate(task, 50, 330): ");
  // By using scheduleAtFixedRate(task,delay,period) method isto
  // schedule the task at a constant rate in a
  // repeated manner and starts after the given delay 
  tmr.scheduleAtFixedRate(task, 100, 800);
 }
}

class ScheduleTask extends TimerTask {
 // Task defined in this method
 public void run() {
  System.out.println("Out of Stock...Keep Working");
 }
}

Output

tmr.scheduleAtFixedRate(task, 50, 330): 
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.