java.util
Class Timer

java.lang.Object
  extended by java.util.Timer

public class Timer
extends Object

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.

See Also:
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

Timer

public Timer()
Creates a new Timer with a non daemon Thread as Scheduler, with normal priority and a default name.


Timer

public 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

public Timer(String name)
Create a new Timer whose Thread has the indicated name. It will have normal priority and will not be a daemon thread.

Parameters:
name - the name of the Thread
Since:
1.5

Timer

public Timer(String name,
             boolean daemon)
Create a new Timer whose Thread has the indicated name. It will have normal priority. The boolean argument controls whether or not it will be a daemon thread.

Parameters:
name - the name of the Thread
daemon - true if the Thread should be a daemon thread
Since:
1.5
Method Detail

cancel

public void cancel()
Cancels the execution of the scheduler. If a task is executing it will normally finish execution, but no other tasks will be executed and no more tasks can be scheduled.


schedule

public void schedule(TimerTask task,
                     long delay)
Schedules the task after the specified delay milliseconds for one time execution.

Throws:
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 died

schedule

public 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.

Throws:
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 died

scheduleAtFixedRate

public 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.

Throws:
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 died

purge

public int purge()
Removes all cancelled tasks from the queue.

Returns:
the number of tasks removed
Since:
1.5