FileDocCategorySizeDatePackage
Timer.javaAPI DocAndroid 1.5 API19820Wed May 06 22:41:04 BST 2009java.util

Timer

public class Timer extends Object
{@code Timer}s are used to schedule jobs for execution in a background process. A single thread is used for the scheduling and this thread has the option of being a daemon thread. By calling {@code cancel} you can terminate a {@code Timer} and its associated thread. All tasks which are scheduled to run after this point are cancelled. Tasks are executed sequentially but are subject to the delays from other tasks run methods. If a specific task takes an excessive amount of time to run it may impact the time at which subsequent tasks may run.

The {@code TimerTask} does not offer any guarantees about the real-time nature of scheduling tasks as its underlying implementation relies on the {@code Object.wait(long)} method.

Multiple threads can share a single {@code Timer} without the need for their own synchronization.

A {@code Timer} can be set to schedule tasks either at a fixed rate or with a fixed period. Fixed-period execution is the default.

The difference between fixed-rate and fixed-period execution is the following: With fixed-rate execution, the start time of each successive run of the task is scheduled in absolute terms without regard for when the previous task run actually took place. This can result in a series of bunched-up runs (one launched immediately after another) if busy resources or other system delays prevent the {@code Timer} from firing for an extended time. With fixed-period execution, each successive run of the task is scheduled relative to the start time of the previous run of the task, so two runs of the task are never fired closer together in time than the specified {@code period}.

see
TimerTask
see
java.lang.Object#wait(long)
since
Android 1.0

Fields Summary
private TimerImpl
impl
private Object
finalizer
Constructors Summary
public Timer(boolean isDaemon)
Creates a new {@code Timer} which may be specified to be run as a daemon thread.

param
isDaemon {@code true} if the {@code Timer}'s thread should be a daemon thread.
since
Android 1.0


                                                      
       
        // BEGIN android-changed
        impl = new TimerImpl("java.util.Timer", isDaemon);
        // END android-changed
    
public Timer()
Creates a new non-daemon {@code Timer}.

since
Android 1.0

        // BEGIN android-changed
        impl = new TimerImpl("java.util.Timer", false);
        // END android-changed
    
public Timer(String name, boolean isDaemon)
Creates a new named {@code Timer} which may be specified to be run as a daemon thread.

param
name the name of the {@code Timer}.
param
isDaemon true if {@code Timer}'s thread should be a daemon thread.
since
Android 1.0

        impl = new TimerImpl(name, isDaemon);
    
public Timer(String name)
Creates a new named {@code Timer} which does not run as a daemon thread.

param
name the name of the Timer.
since
Android 1.0

        impl = new TimerImpl(name, false);
    
Methods Summary
public voidcancel()
Cancels the {@code Timer} and removes any scheduled tasks. If there is a currently running task it is not affected. No more tasks may be scheduled on this {@code Timer}. Subsequent calls do nothing.

since
Android 1.0

        impl.cancel();
    
public intpurge()
Removes all canceled tasks from the task queue. If there are no other references on the tasks, then after this call they are free to be garbage collected.

return
the number of canceled tasks that were removed from the task queue.
since
Android 1.0

        synchronized (impl) {
            return impl.purge();
        }
    
public voidschedule(java.util.TimerTask task, java.util.Date when, long period)
Schedule a task for repeated fixed-delay execution after a specific time has been reached.

param
task the task to schedule.
param
when time of first execution.
param
period amount of time between subsequent executions.
exception
IllegalArgumentException if {@code when.getTime() < 0} or {@code period < 0}.
exception
IllegalStateException if the {@code Timer} has been canceled, or if the task has been scheduled or canceled.
since
Android 1.0

        if (period <= 0 || when.getTime() < 0) {
            throw new IllegalArgumentException();
        }
        long delay = when.getTime() - System.currentTimeMillis();
        scheduleImpl(task, delay < 0 ? 0 : delay, period, false);
    
public voidschedule(java.util.TimerTask task, java.util.Date when)
Schedule a task for single execution. If {@code when} is less than the current time, it will be scheduled to be executed as soon as possible.

param
task the task to schedule.
param
when time of execution.
exception
IllegalArgumentException if {@code when.getTime() < 0}.
exception
IllegalStateException if the {@code Timer} has been canceled, or if the task has been scheduled or canceled.
since
Android 1.0

        if (when.getTime() < 0) {
            throw new IllegalArgumentException();
        }
        long delay = when.getTime() - System.currentTimeMillis();
        scheduleImpl(task, delay < 0 ? 0 : delay, -1, false);
    
public voidschedule(java.util.TimerTask task, long delay)
Schedule a task for single execution after a specified delay.

param
task the task to schedule.
param
delay amount of time before execution.
exception
IllegalArgumentException if {@code delay < 0}.
exception
IllegalStateException if the {@code Timer} has been canceled, or if the task has been scheduled or canceled.
since
Android 1.0

        if (delay < 0) {
            throw new IllegalArgumentException();
        }
        scheduleImpl(task, delay, -1, false);
    
public voidschedule(java.util.TimerTask task, long delay, long period)
Schedule a task for repeated fixed-delay execution after a specific delay.

param
task the task to schedule.
param
delay amount of time before first execution.
param
period amount of time between subsequent executions.
exception
IllegalArgumentException if {@code delay < 0} or {@code period < 0}.
exception
IllegalStateException if the {@code Timer} has been canceled, or if the task has been scheduled or canceled.
since
Android 1.0

        if (delay < 0 || period <= 0) {
            throw new IllegalArgumentException();
        }
        scheduleImpl(task, delay, period, false);
    
public voidscheduleAtFixedRate(java.util.TimerTask task, long delay, long period)
Schedule a task for repeated fixed-rate execution after a specific delay has passed.

param
task the task to schedule.
param
delay amount of time before first execution.
param
period amount of time between subsequent executions.
exception
IllegalArgumentException if {@code delay < 0} or {@code period < 0}.
exception
IllegalStateException if the {@code Timer} has been canceled, or if the task has been scheduled or canceled.
since
Android 1.0

        if (delay < 0 || period <= 0) {
            throw new IllegalArgumentException();
        }
        scheduleImpl(task, delay, period, true);
    
public voidscheduleAtFixedRate(java.util.TimerTask task, java.util.Date when, long period)
Schedule a task for repeated fixed-rate execution after a specific time has been reached.

param
task the task to schedule.
param
when time of first execution.
param
period amount of time between subsequent executions.
exception
IllegalArgumentException if {@code when.getTime() < 0} or {@code period < 0}.
exception
IllegalStateException if the {@code Timer} has been canceled, or if the task has been scheduled or canceled.
since
Android 1.0

        if (period <= 0 || when.getTime() < 0) {
            throw new IllegalArgumentException();
        }
        long delay = when.getTime() - System.currentTimeMillis();
        scheduleImpl(task, delay < 0 ? 0 : delay, period, true);
    
private voidscheduleImpl(java.util.TimerTask task, long delay, long period, boolean fixed)
Schedule a task.

param
task
param
delay
param
period
param
fixed

        synchronized (impl) {
            if (impl.cancelled) {
                throw new IllegalStateException(Msg.getString("K00f3")); //$NON-NLS-1$
            }

            long when = delay + System.currentTimeMillis();

            if (when < 0) {
                throw new IllegalArgumentException(Msg.getString("K00f5")); //$NON-NLS-1$
            }

            synchronized (task.lock) {
                if (task.isScheduled()) {
                    throw new IllegalStateException(Msg.getString("K00f6")); //$NON-NLS-1$
                }

                if (task.cancelled) {
                    throw new IllegalStateException(Msg.getString("K00f7")); //$NON-NLS-1$
                }

                task.when = when;
                task.period = period;
                task.fixedRate = fixed;
            }

            // insert the newTask into queue
            impl.insertTask(task);
        }