How to use java.util.Timer to repeat the task

by toy

This is how I do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Timer;

import java.util.TimerTask;

/**
 *
 * This class uses the java.util.Timer functionality
 * The class will repeat itself every second.
 *
 * @author Toy
 */


public class Main
{
    public static void main(String[] args) 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("done");
                }
            }, delay, period);
    }
}