Timer() constructor from Timer has the following syntax.
public Timer()
In the following code shows how to use Timer.Timer() constructor.
The following code sets up a timer with an initial delay of 1 second, then repeat every half second.
import java.util.Timer; import java.util.TimerTask; //from ww w . jav a2 s.com class MyTimerTask extends TimerTask { public void run() { System.out.println("Timer task executed."); } } public class Main { public static void main(String args[]) { MyTimerTask myTask = new MyTimerTask(); Timer myTimer = new Timer(); /* * Set an initial delay of 1 second, then repeat every half second. */ myTimer.schedule(myTask, 1000, 500); try { Thread.sleep(5000); } catch (InterruptedException exc) { } myTimer.cancel(); } }
The code above generates the following result.