Name

TimerTask

Synopsis

This abstract Runnable class represents a task that is scheduled with a Timer object for one-time or repeated execution in the future. You can define a task by subclassing TimerTask and implementing the abstract run( ) method. Schedule the task for future execution by passing an instance of your subclass to one of the schedule( ) or scheduleAtFixedRate( ) methods of Timer. The Timer object will then invoke the run( ) method at the scheduled time or times.

Call cancel( ) to cancel the one-time or repeated execution of a TimerTask( ). This method returns true if a pending execution was actually canceled. It returns false if the task has already been canceled, was never scheduled, or was scheduled for one-time execution and has already been executed. scheduledExecutionTime( ) returns the time in milliseconds at which the most recent execution of the TimerTask was scheduled to occur. When the host system is heavily loaded, the run( ) method may not be invoked exactly when scheduled. Some tasks may choose to do nothing if they are not invoked on time. The run( ) method can compare the return values of scheduledExecutionTime( ) and System.currentTimeMillis( ) to determine whether the current invocation is sufficiently timely.

java.util.TimerTask

Figure 16-60. java.util.TimerTask

public abstract class TimerTask implements Runnable {
// Protected Constructors
     protected TimerTask( );  

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.