Here is a Java Program to Demonstrate Timer and TimerTask.
Output of Above Java Program
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
import java.util.*; class MyTimerTask extends TimerTask { public void run() { System.out.println("Timer task executed."); } } class TTest { 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(); } }
Output of Above Java Program
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.
Timer task executed.