Timer and TimerTask class in java

Timer and TimerTask work together. Timer is the class that you will use to schedule a task for execution. The task being scheduled must be an instance of TimerTask. Thus, to schedule a task, you will first create a TimerTask object and then schedule it for execution using an instance of Timer

TimerTask implements the Runnable interface; thus, it can be used to create a thread of execution. Its constructor is shown here:

TimerTask( )

The Methods Defined by TimerTask

 MethodDescription
 boolean cancel( )Terminates the task. Returns true if an execution of the task is
  prevented. Otherwise, returns false.
   
 abstract void run( )Contains the code for the timer task.
   
 long scheduledExecutionTime( )Returns the time at which the last execution of the task was
  scheduled to have occurred.
MethodDescription 
void cancel( )Cancels the timer thread. 
   
int purge( )Deletes cancelled tasks from the timer’s queue. 
   
void schedule(TimerTask TTask,TTask is scheduled for execution after the period passed 
long wait)in wait has elapsed. The wait parameter is specified in 
 milliseconds. 
void schedule(TimerTask TTask,TTask is scheduled for execution after the period passed 
long wait, long repeat)in wait has elapsed. The task is then executed repeatedly 
 at the interval specified by repeat. Both wait and repeat 
 are specified in milliseconds. 
void schedule(TimerTask TTask,TTask is scheduled for execution at the time specified 
Date targetTime)by targetTime. 
void schedule(TimerTask TTask,TTask is scheduled for execution at the time specified 
Date targetTime,by targetTime. The task is then executed repeatedly at 
long repeat)the interval passed in repeat. The repeat parameter is 
 specified in milliseconds. 
void scheduleAtFixedRate(TTask is scheduled for execution after the period passed 
TimerTask TTask,in wait has elapsed. The task is then executed repeatedly 
long wait, long repeat)at the interval specified by repeat. Both wait and repeat 
 are specified in milliseconds. The time of each repetition is 
 relative to the first execution, not the preceding execution. 
 Thus, the overall rate of execution is fixed. 
   
void scheduleAtFixedRate(TTask is scheduled for execution at the time specified 
TimerTask TTask,by targetTime. The task is then executed repeatedly at 
Date targetTime,the interval passed in repeat. The repeat parameter is 
long repeat)specified in milliseconds. The time of each repetition is 
 relative to the first execution, not the preceding execution. 
 Thus, the overall rate of execution is fixed. 

Demonstrate Timer and TimerTask

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();

}

}

Leave a Comment