Java Utility Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java.util.Timer Class



Java Timer Class

Java.util package has a Timer class which provides a 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. It is thread-safe class, i.e, multiple threads can share a single Timer object without the need for external synchronization. Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.

Class declaration

The declaration of java.util.Timer class is:

public class Timer
  extends Object

Class Constructors

S.NConstructors & Description
1. Timer()
Creates a new timer.
2. Timer(boolean isDaemon)
Creates a new timer whose associated thread may be specified to run as a daemon.
3. Timer(String name)
Creates a new timer whose associated thread has the specified name.
4. 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.

java.util.Timer Methods

The java.util.Timer class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. void cancel()
Terminates the timer, discarding any currently scheduled tasks.
2. int purge()
Removes all cancelled tasks from this timer's task queue.
3. void schedule(TimerTask task, Date time)
Schedules the specified task for execution at the specified time.
4. void schedule(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified time.
5. void schedule(TimerTask task, long delay)
Schedules the specified task for execution after the specified delay.
6. void schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
7. void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
Schedules the specified task for repeated fixed-rate execution, beginning at the specified time.
8. void scheduleAtFixedRate(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object

5