java.util.Timer is a utility class that provides the facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.
java.util.TimerTask is an abstract class which implements Runnable interface. We need to extend this class to create our own TimerTask which can be scheduled using java Timer class. A task that can be scheduled to run once or for the repeated number of executions. Each timer object is associated with a background thread that is responsible for the execution of all the tasks of timer object.
Run a reminder timer task after a certain time period
Perform timer task repeatedly
Few Constructors of Timer
Creates a new timer
Creates a new timer whose associated thread may be specified to run as a daemon.
Creates a new timer whose associated thread has the specified name.
Timer(String name, boolean isDaemon)
Creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon.
Every class in Java inherit all the methods of the Object class. Methods which inherited from the Object class are:
Timer class Methods are,
java.util.Timer.cancel() Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it
public void cancel()
java.util.Timer.purge() Removes all canceled tasks from this timer’s task queue
public int purge()
the number of tasks removed from the queue
schedule(TimerTask task, Date time)
java.util.Timer.schedule(TimerTask task, Date time) Schedules the specified task for execution at the specified time
public void schedule(TimerTask task, Date time)
task - task to be scheduled.
time - time at which task is to be executed.
IllegalArgumentException - if time.getTime() is negative.
IllegalStateException - if task was already scheduled or canceled,
the timer was canceled, or timer thread terminated.
NullPointerException - if task or time is null
schedule(TimerTask task, Date firstTime, long period)
java.util.Timer.schedule(TimerTask task, Date firstTime, long period) Schedules the specified task for repeated fixed-delay execution, beginning at the specified time
public void schedule(TimerTask task, Date firstTime, long period)
task - task to be scheduled.
firsttime - First time at which task is to be executed.
period - time in milliseconds between successive task executions.
IllegalArgumentException - if firstTime.getTime() < 0,
or period <= 0
IllegalStateException - if task was already scheduled
or cancelled, timer was cancelled,
or timer thread terminated.
NullPointerException - if task or firstTime is null
Deep dive in the Timer
1. Timer class is the thread-safe so multiple threads can share a single Timer object without the need for external synchronization.
2. Timer class uses java.util.TaskQueue to add tasks at given regular interval and at any time there can be only one thread running the TimerTask. TaskQueue uses binary heap data structure in order to store its task.
4. Timer class uses Object wait and notify methods to schedule the tasks.
5. Java Timer object can be created to run the associated tasks as a daemon thread. Timer cancel() method is used to terminate the timer and discard any scheduled tasks, however, it doesn’t interfere with the currently executing task and let it finish. If the timer is run as the daemon thread, whether we cancel it or not, it will terminate as soon as all the user threads are finished executing.
6. While scheduling tasks using Timer, you should make sure that time interval is more than normal thread execution, otherwise, tasks queue size will keep growing and eventually task will be executing always.