2 * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
29 * A task that can be scheduled for one-time or repeated execution by a Timer.
36 public abstract class TimerTask implements Runnable {
38 * This object is used to control access to the TimerTask internals.
40 final Object lock = new Object();
43 * The state of this task, chosen from the constants below.
48 * This task has not yet been scheduled.
50 static final int VIRGIN = 0;
53 * This task is scheduled for execution. If it is a non-repeating task,
54 * it has not yet been executed.
56 static final int SCHEDULED = 1;
59 * This non-repeating task has already executed (or is currently
60 * executing) and has not been cancelled.
62 static final int EXECUTED = 2;
65 * This task has been cancelled (with a call to TimerTask.cancel).
67 static final int CANCELLED = 3;
70 * Next execution time for this task in the format returned by
71 * System.currentTimeMillis, assuming this task is scheduled for execution.
72 * For repeating tasks, this field is updated prior to each task execution.
74 long nextExecutionTime;
77 * Period in milliseconds for repeating tasks. A positive value indicates
78 * fixed-rate execution. A negative value indicates fixed-delay execution.
79 * A value of 0 indicates a non-repeating task.
84 * Creates a new timer task.
86 protected TimerTask() {
90 * The action to be performed by this timer task.
92 public abstract void run();
95 * Cancels this timer task. If the task has been scheduled for one-time
96 * execution and has not yet run, or has not yet been scheduled, it will
97 * never run. If the task has been scheduled for repeated execution, it
98 * will never run again. (If the task is running when this call occurs,
99 * the task will run to completion, but will never run again.)
101 * <p>Note that calling this method from within the <tt>run</tt> method of
102 * a repeating timer task absolutely guarantees that the timer task will
105 * <p>This method may be called repeatedly; the second and subsequent
106 * calls have no effect.
108 * @return true if this task is scheduled for one-time execution and has
109 * not yet run, or this task is scheduled for repeated execution.
110 * Returns false if the task was scheduled for one-time execution
111 * and has already run, or if the task was never scheduled, or if
112 * the task was already cancelled. (Loosely speaking, this method
113 * returns <tt>true</tt> if it prevents one or more scheduled
114 * executions from taking place.)
116 public boolean cancel() {
118 boolean result = (state == SCHEDULED);
125 * Returns the <i>scheduled</i> execution time of the most recent
126 * <i>actual</i> execution of this task. (If this method is invoked
127 * while task execution is in progress, the return value is the scheduled
128 * execution time of the ongoing task execution.)
130 * <p>This method is typically invoked from within a task's run method, to
131 * determine whether the current execution of the task is sufficiently
132 * timely to warrant performing the scheduled activity:
134 * public void run() {
135 * if (System.currentTimeMillis() - scheduledExecutionTime() >=
137 * return; // Too late; skip this execution.
138 * // Perform the task
141 * This method is typically <i>not</i> used in conjunction with
142 * <i>fixed-delay execution</i> repeating tasks, as their scheduled
143 * execution times are allowed to drift over time, and so are not terribly
146 * @return the time at which the most recent execution of this task was
147 * scheduled to occur, in the format returned by Date.getTime().
148 * The return value is undefined if the task has yet to commence
149 * its first execution.
150 * @see Date#getTime()
152 public long scheduledExecutionTime() {
154 return (period < 0 ? nextExecutionTime + period
155 : nextExecutionTime - period);