Java.util.Timer Class

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

S.N.Constructor & Description
1Timer()

This constructor creates a new timer.

2Timer(boolean isDaemon)

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

3Timer(String name)

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

4Timer(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

S.N.Method & Description
1void cancel()

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

2int purge()

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

3void schedule(TimerTask task, Date time)

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

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

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

5void schedule(TimerTask task, long delay)

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

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

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

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

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

8void 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

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.