Schedule at fixed rate for Timer in Java

Description

The following code shows how to schedule at fixed rate for Timer.

Example


/* ww w.  j a v  a 2s.  c  o m*/
import java.util.Timer;
import java.util.TimerTask;

public class Main {
  public static void main(String[] argv) throws Exception {
    int delay = 5000; // delay for 5 sec.
    int period = 1000; // repeat every sec.
    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
        System.out.println("doing"); 
      }
    }, delay, period);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Development »




Java Algorithms
Java Clipboard
Java Compiler
Java Desktop
Java Virtual Machine
Java Math
OS
Random
Java Robot
Java RuntimeMXBean
Java Timer
Java UUID
Java Internationalization