public class Timer
extends java.lang.Object
Constructor and Description |
---|
Timer()
Creates a new timer.
|
Timer(boolean automatic)
Creates a new timer.
|
Modifier and Type | Method and Description |
---|---|
void |
cancel()
Terminates this timer, discarding any currently scheduled tasks.
|
void |
run()
Executes
TimerTask scheduling. |
void |
schedule(TimerTask task,
java.util.Date time)
Schedules the specified task for execution at the specified time.
|
void |
schedule(TimerTask task,
java.util.Date firstTime,
long period)
Schedules the specified task for repeated fixed-delay execution, beginning at the specified
time.
|
void |
schedule(TimerTask task,
long delay)
Schedules the specified task for execution after the specified delay.
|
void |
schedule(TimerTask task,
long delay,
long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the
specified delay.
|
void |
scheduleAtFixedRate(TimerTask task,
java.util.Date firstTime,
long period)
Schedules the specified task for repeated fixed-rate execution, beginning at the specified
time.
|
void |
scheduleAtFixedRate(TimerTask task,
long delay,
long period)
Schedules the specified task for repeated fixed-rate execution, beginning after the specified
delay.
|
public Timer()
public Timer(boolean automatic)
Creates a new timer.
If the given boolean is true
, a thread is created to execute this timer. The associated thread
does not run as a daemon thread, which may prevent an application from terminating. All the TimerTask
scheduled by the timer will be executed in the context of the associated thread.
Otherwise an applicative thread must call the run()
method in order
to execute tasks scheduling.
automatic
- If true
a thread is created to run this timer, otherwise the application must manage this timer.run()
public void schedule(TimerTask task, long delay)
task
- task to be scheduled.delay
- delay in milliseconds before task is to be executed. Note that the actual delay
may be different from the amount requested since the resolution of the Timer is
implementation and device dependent.java.lang.IllegalArgumentException
- if delay is negative, or delay + CurrentTime.get() is negative.IllegalStateException
- if task was already scheduled or canceled, or timer was canceled.public void schedule(TimerTask task, java.util.Date time)
task
- task to be scheduled.time
- time at which task is to be executed.java.lang.IllegalArgumentException
- if time.getTime() is negative.IllegalStateException
- if task was already scheduled or canceled, timer was canceled, or timer thread
terminated.public void schedule(TimerTask task, long delay, long period)
task
- task to be scheduled.delay
- delay in milliseconds before task is to be executed. Note that the actual delay
may be different from the amount requested since the resolution of the Timer is
implementation and device dependent.period
- time in milliseconds between successive task executions.java.lang.IllegalArgumentException
- if delay is negative, or delay + CurrentTime.get() is negative.IllegalStateException
- if task was already scheduled or canceled, timer was canceled, or timer thread
terminated.public void schedule(TimerTask task, java.util.Date firstTime, long period)
task
- task to be scheduled.firstTime
- First time at which task is to be executed.period
- time in milliseconds between successive task executions.java.lang.IllegalArgumentException
- if time.getTime() is negative.IllegalStateException
- if task was already scheduled or canceled, timer was canceled, or timer thread
terminated.public void scheduleAtFixedRate(TimerTask task, long delay, long period)
task
- task to be scheduled.delay
- delay in milliseconds before task is to be executed. Note that the actual delay
may be different from the amount requested since the resolution of the Timer is
implementation and device dependent.period
- time in milliseconds between successive task executions.java.lang.IllegalArgumentException
- if delay is negative, or delay + CurrentTime.get() is negative.IllegalStateException
- if task was already scheduled or canceled, timer was canceled, or timer thread
terminated.public void scheduleAtFixedRate(TimerTask task, java.util.Date firstTime, long period)
task
- task to be scheduled.firstTime
- first time at which task is to be executed.period
- time in milliseconds between successive task executions.java.lang.IllegalArgumentException
- if time.getTime() is negative.IllegalStateException
- if task was already scheduled or canceled, timer was canceled, or timer thread
terminated.public void cancel()
public void run()
Executes TimerTask
scheduling. This method must be called only for Timer
that are
not automatic (i.e. no thread is automatically started at Timer
creation). This is the current
thread that executes the Timer
scheduling loop.
This method can be called once on this Timer
and returns only if cancel()
is called.
IllegalStateException
- if this timer is already running or canceled.