Java.util.Timer Class



Introduction

The java.util.Timer class provides facility for threads to schedule tasks for future execution in a background thread.

  • This class is thread-safe i.e multiple threads can share a single Timer object without the need for external synchronization.

  • This class schedules tasks for one-time execution, or for repeated execution at regular intervals.

  • All constructors start a timer thread.

Class declaration

Following is the declaration for java.util.Timer class −

public class Timer
   extends Object

Class constructors

Sr.No. Constructor & Description
1

Timer()

This constructor creates a new timer.

2

Timer(boolean isDaemon)

This constructor creates a new timer whose associated thread may be specified to run as a daemon.

3

Timer(String name)

This constructor creates a new timer whose associated thread has the specified name.

4

Timer(String name, boolean isDaemon)

This constructor creates a new timer whose associated thread has the specified name, and may be specified to run as a daemon.

Class methods

Sr.No. Method & Description
1 void cancel()

This method terminates this timer, discarding any currently scheduled tasks.

2 int purge()

This method removes all cancelled tasks from this timer's task queue.

3 void schedule(TimerTask task, Date time)

This method schedules the specified task for execution at the specified time.

4 void schedule(TimerTask task, Date firstTime, long period)

This method schedules the specified task for repeated fixed-delay execution, beginning at the specified time.

5 void schedule(TimerTask task, long delay)

This method schedules the specified task for execution after the specified delay.

6 void schedule(TimerTask task, long delay, long period)

This method schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.

7 void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)

This method schedules the specified task for repeated fixed-rate execution, beginning at the specified time.

8 void scheduleAtFixedRate(TimerTask task, long delay, long period)

This method Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.

Methods inherited

This class inherits methods from the following classes −

  • java.util.Object
Advertisements