Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Timer

public class Timer
extends Object

java.lang.Object
   ↳ java.util.Timer


Timers schedule one-shot or recurring tasks for execution. Prefer ScheduledThreadPoolExecutor for new code.

Each timer has one thread on which tasks are executed sequentially. When this thread is busy running a task, runnable tasks may be subject to delays.

One-shot tasks are scheduled to run at an absolute time or after a relative delay.

Recurring tasks are scheduled with either a fixed period or a fixed rate:

When a timer is no longer needed, users should call cancel(), which releases the timer's thread and other resources. Timers not explicitly cancelled may hold resources indefinitely.

This class does not offer guarantees about the real-time nature of task scheduling. Multiple threads can share a single timer without synchronization.

Summary

Public constructors

Timer(String name, boolean isDaemon)

Creates a new named Timer which may be specified to be run as a daemon thread.

Timer(String name)

Creates a new named Timer which does not run as a daemon thread.

Timer(boolean isDaemon)

Creates a new Timer which may be specified to be run as a daemon thread.

Timer()

Creates a new non-daemon Timer.

Public methods

void cancel()

Cancels the Timer and all scheduled tasks.

int purge()

Removes all canceled tasks from the task queue.

void schedule(TimerTask task, Date when, long period)

Schedule a task for repeated fixed-delay execution after a specific time has been reached.

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

Schedule a task for repeated fixed-delay execution after a specific delay.

void schedule(TimerTask task, Date when)

Schedule a task for single execution.

void schedule(TimerTask task, long delay)

Schedule a task for single execution after a specified delay.

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

Schedule a task for repeated fixed-rate execution after a specific delay has passed.

void scheduleAtFixedRate(TimerTask task, Date when, long period)

Schedule a task for repeated fixed-rate execution after a specific time has been reached.

Inherited methods

From class java.lang.Object

Public constructors

Timer

Added in API level 1
Timer (String name, 
                boolean isDaemon)

Creates a new named Timer which may be specified to be run as a daemon thread.

Parameters
name String
isDaemon boolean
Throws
NullPointerException if name == null

Timer

Added in API level 1
Timer (String name)

Creates a new named Timer which does not run as a daemon thread.

Parameters
name String
Throws
NullPointerException if name == null

Timer

Added in API level 1
Timer (boolean isDaemon)

Creates a new Timer which may be specified to be run as a daemon thread.

Parameters
isDaemon boolean: true if the Timer's thread should be a daemon thread.

Timer

Added in API level 1
Timer ()

Creates a new non-daemon Timer.

Public methods

cancel

Added in API level 1
void cancel ()

Cancels the Timer and all scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this Timer. Subsequent calls do nothing.

purge

Added in API level 1
int purge ()

Removes all canceled tasks from the task queue. If there are no other references on the tasks, then after this call they are free to be garbage collected.

Returns
int the number of canceled tasks that were removed from the task queue.

schedule

Added in API level 1
void schedule (TimerTask task, 
                Date when, 
                long period)

Schedule a task for repeated fixed-delay execution after a specific time has been reached.

Parameters
task TimerTask: the task to schedule.
when Date: time of first execution.
period long: amount of time in milliseconds between subsequent executions.
Throws
IllegalArgumentException if when.getTime() < 0 or period <= 0.
IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.

schedule

Added in API level 1
void schedule (TimerTask task, 
                long delay, 
                long period)

Schedule a task for repeated fixed-delay execution after a specific delay.

Parameters
task TimerTask: the task to schedule.
delay long: amount of time in milliseconds before first execution.
period long: amount of time in milliseconds between subsequent executions.
Throws
IllegalArgumentException if delay < 0 or period <= 0.
IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.

schedule

Added in API level 1
void schedule (TimerTask task, 
                Date when)

Schedule a task for single execution. If when is less than the current time, it will be scheduled to be executed as soon as possible.

Parameters
task TimerTask: the task to schedule.
when Date: time of execution.
Throws
IllegalArgumentException if when.getTime() < 0.
IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.

schedule

Added in API level 1
void schedule (TimerTask task, 
                long delay)

Schedule a task for single execution after a specified delay.

Parameters
task TimerTask: the task to schedule.
delay long: amount of time in milliseconds before execution.
Throws
IllegalArgumentException if delay < 0.
IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.

scheduleAtFixedRate

Added in API level 1
void scheduleAtFixedRate (TimerTask task, 
                long delay, 
                long period)

Schedule a task for repeated fixed-rate execution after a specific delay has passed.

Parameters
task TimerTask: the task to schedule.
delay long: amount of time in milliseconds before first execution.
period long: amount of time in milliseconds between subsequent executions.
Throws
IllegalArgumentException if delay < 0 or period <= 0.
IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.

scheduleAtFixedRate

Added in API level 1
void scheduleAtFixedRate (TimerTask task, 
                Date when, 
                long period)

Schedule a task for repeated fixed-rate execution after a specific time has been reached.

Parameters
task TimerTask: the task to schedule.
when Date: time of first execution.
period long: amount of time in milliseconds between subsequent executions.
Throws
IllegalArgumentException if when.getTime() < 0 or period <= 0.
IllegalStateException if the Timer has been canceled, or if the task has been scheduled or canceled.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.