|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.util.Timer
public class Timer
A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. Corresponding to each Timer object is a single background thread that is used to execute all of the timer's tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it "hogs" the timer's task execution thread. This can, in turn, delay the execution of subsequent tasks, which may "bunch up" and execute in rapid succession when (and if) the offending task finally completes.
After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer's task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer's task execution thread rapidly, the caller should invoke the timer's cancel method.
If the timer's task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer's cancel method had been invoked.
This class is thread-safe: multiple threads can share a single Timer object without the need for external synchronization.
This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.
Timers function only within a single VM and are canceled when the VM exits. When the VM is started no timers exist, they are created only by application request.
TimerTask
Constructor Summary | |
---|---|
Timer()
Creates a new Timer with a non daemon Thread as Scheduler, with normal priority and a default name. |
|
Timer(boolean daemon)
Creates a new Timer with a daemon Thread as scheduler if daemon is true, with normal priority and a default name. |
|
Timer(String name)
Create a new Timer whose Thread has the indicated name. |
|
Timer(String name,
boolean daemon)
Create a new Timer whose Thread has the indicated name. |
Method Summary | |
---|---|
void |
cancel()
Cancels the execution of the scheduler. |
int |
purge()
Removes all cancelled tasks from the queue. |
void |
schedule(TimerTask task,
long delay)
Schedules the task after the specified delay milliseconds for one time execution. |
void |
schedule(TimerTask task,
long delay,
long period)
Schedules the task after the delay milliseconds and reschedules the task every period milliseconds after the last execution of the task finishes until this timer or the task is canceled. |
void |
scheduleAtFixedRate(TimerTask task,
long delay,
long period)
Schedules the task after the delay milliseconds and reschedules the task at a fixed rate every period milliseconds until this timer or the task is canceled. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Timer()
public Timer(boolean daemon)
public Timer(String name)
name
- the name of the Threadpublic Timer(String name, boolean daemon)
name
- the name of the Threaddaemon
- true if the Thread should be a daemon threadMethod Detail |
---|
public void cancel()
public void schedule(TimerTask task, long delay)
IllegalArgumentException
- if delay or
System.currentTimeMillis + delay is negative
IllegalStateException
- if the task was already scheduled or
canceled or this Timer is canceled or the scheduler thread has diedpublic void schedule(TimerTask task, long delay, long period)
IllegalArgumentException
- if delay or period is negative
IllegalStateException
- if the task was already scheduled or
canceled or this Timer is canceled or the scheduler thread has diedpublic void scheduleAtFixedRate(TimerTask task, long delay, long period)
IllegalArgumentException
- if delay or
System.currentTimeMillis + delay is negative
IllegalStateException
- if the task was already scheduled or
canceled or this Timer is canceled or the scheduler thread has diedpublic int purge()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |