public class Timer extends Object
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 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:
period
.
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.
Constructor and Description |
---|
Timer()
Creates a new non-daemon
Timer . |
Timer(boolean isDaemon)
Creates a new
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(String name,
boolean isDaemon)
Creates a new named
Timer which may be specified to be run as a
daemon thread. |
Modifier and Type | Method and Description |
---|---|
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)
Schedule a task for single execution.
|
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)
Schedule a task for single execution after a specified delay.
|
void |
schedule(TimerTask task,
long delay,
long period)
Schedule a task for repeated fixed-delay execution after a specific delay.
|
void |
scheduleAtFixedRate(TimerTask task,
Date when,
long period)
Schedule a task for repeated fixed-rate execution after a specific time
has been reached.
|
void |
scheduleAtFixedRate(TimerTask task,
long delay,
long period)
Schedule a task for repeated fixed-rate execution after a specific delay
has passed.
|
public Timer(String name, boolean isDaemon)
Timer
which may be specified to be run as a
daemon thread.name
- the name of the Timer
.isDaemon
- true if Timer
's thread should be a daemon thread.NullPointerException
- is name
is null
public Timer(String name)
Timer
which does not run as a daemon thread.name
- the name of the Timer.NullPointerException
- is name
is null
public Timer(boolean isDaemon)
Timer
which may be specified to be run as a daemon thread.isDaemon
- true
if the Timer
's thread should be a daemon thread.public Timer()
Timer
.public void cancel()
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.public int purge()
public void schedule(TimerTask task, Date when)
when
is less than the
current time, it will be scheduled to be executed as soon as possible.task
- the task to schedule.when
- time of execution.IllegalArgumentException
- if when.getTime() < 0
.IllegalStateException
- if the Timer
has been canceled, or if the task has been
scheduled or canceled.public void schedule(TimerTask task, long delay)
task
- the task to schedule.delay
- amount of time in milliseconds before execution.IllegalArgumentException
- if delay < 0
.IllegalStateException
- if the Timer
has been canceled, or if the task has been
scheduled or canceled.public void schedule(TimerTask task, long delay, long period)
task
- the task to schedule.delay
- amount of time in milliseconds before first execution.period
- amount of time in milliseconds between subsequent executions.IllegalArgumentException
- if delay < 0
or period <= 0
.IllegalStateException
- if the Timer
has been canceled, or if the task has been
scheduled or canceled.public void schedule(TimerTask task, Date when, long period)
task
- the task to schedule.when
- time of first execution.period
- amount of time in milliseconds between subsequent executions.IllegalArgumentException
- if when.getTime() < 0
or period <= 0
.IllegalStateException
- if the Timer
has been canceled, or if the task has been
scheduled or canceled.public void scheduleAtFixedRate(TimerTask task, long delay, long period)
task
- the task to schedule.delay
- amount of time in milliseconds before first execution.period
- amount of time in milliseconds between subsequent executions.IllegalArgumentException
- if delay < 0
or period <= 0
.IllegalStateException
- if the Timer
has been canceled, or if the task has been
scheduled or canceled.public void scheduleAtFixedRate(TimerTask task, Date when, long period)
task
- the task to schedule.when
- time of first execution.period
- amount of time in milliseconds between subsequent executions.IllegalArgumentException
- if when.getTime() < 0
or period <= 0
.IllegalStateException
- if the Timer
has been canceled, or if the task has been
scheduled or canceled.