public class Timer extends Object
Each timer is associated with precisely one “execution thread”, which it uses to run
TimerTask instances serially. Scheduled TimerTask instances may either be
one-shot or recurring. Recurring tasks can be scheduled as fixed-period with schedule(java.util.TimerTask, java.util.Date)
or as fixed-rate with scheduleAtFixedRate(java.util.TimerTask, long, long).
| Constructor and Description |
|---|
Timer()
Equivalent to
this(false) |
Timer(boolean isDaemon)
Creates a new timer with a default (unique) name.
|
Timer(String name)
Equivalent to
this(name, false). |
Timer(String name,
boolean isDaemon)
Create a new timer with name
name. |
| Modifier and Type | Method and Description |
|---|---|
void |
cancel()
Cancels all remaining tasks on the timer.
|
protected void |
finalize()
Make sure the thread is stopped when the timer is garbage-collected.
|
int |
purge()
Removes all references held by this instance to cancelled timer-tasks.
|
void |
schedule(TimerTask task,
Date when)
Schedules a one-shot task at a fixed time
when. |
void |
schedule(TimerTask task,
Date when,
long period)
Schedules a recurring fixed-period task after a fixed time
when with period
period. |
void |
schedule(TimerTask task,
long delay)
Schedules a one-shot task after a fixed delay
delay, specified in milliseconds. |
void |
schedule(TimerTask task,
long delay,
long period)
Schedules a recurring fixed-period task after a fixed delay
delay
with period period. |
void |
scheduleAtFixedRate(TimerTask task,
Date when,
long period)
Schedules a recurring fixed-rate task after a fixed time
when
with period period. |
void |
scheduleAtFixedRate(TimerTask task,
long delay,
long period)
Schedules a recurring fixed-rate task after a fixed delay
delay
with period period. |
public Timer(String name, boolean isDaemon)
name. isDaemon specifies whether the
execution thread is a daemon or not.public Timer(String name)
this(name, false).public Timer(boolean isDaemon)
public Timer()
this(false)protected void finalize()
throws Throwable
public void cancel()
The task that’s currently running (if any) must be allowed to complete. No further tasks can be enqueued on this timer. Also, releases all resources associated with this timer. All subsequent calls to methods on this instance must be no-ops.
public int purge()
Those tasks will be collectible at the end of this method if no other references to them exist. Returns the number of tasks purged.
public void schedule(TimerTask task, Date when)
when.public void schedule(TimerTask task, long delay)
delay, specified in milliseconds.public void schedule(TimerTask task, long delay, long period)
delay
with period period.
Both delay and period are specified in milliseconds.
public void schedule(TimerTask task, Date when, long period)
when with period
period.
Period is specified in milliseconds.
public void scheduleAtFixedRate(TimerTask task, long delay, long period)
delay
with period period.
Both delay and period are specified in milliseconds.