0N/A/*
2362N/A * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util;
0N/A
0N/A/**
0N/A * A task that can be scheduled for one-time or repeated execution by a Timer.
0N/A *
0N/A * @author Josh Bloch
0N/A * @see Timer
0N/A * @since 1.3
0N/A */
0N/A
0N/Apublic abstract class TimerTask implements Runnable {
0N/A /**
0N/A * This object is used to control access to the TimerTask internals.
0N/A */
0N/A final Object lock = new Object();
0N/A
0N/A /**
0N/A * The state of this task, chosen from the constants below.
0N/A */
0N/A int state = VIRGIN;
0N/A
0N/A /**
0N/A * This task has not yet been scheduled.
0N/A */
0N/A static final int VIRGIN = 0;
0N/A
0N/A /**
0N/A * This task is scheduled for execution. If it is a non-repeating task,
0N/A * it has not yet been executed.
0N/A */
0N/A static final int SCHEDULED = 1;
0N/A
0N/A /**
0N/A * This non-repeating task has already executed (or is currently
0N/A * executing) and has not been cancelled.
0N/A */
0N/A static final int EXECUTED = 2;
0N/A
0N/A /**
0N/A * This task has been cancelled (with a call to TimerTask.cancel).
0N/A */
0N/A static final int CANCELLED = 3;
0N/A
0N/A /**
0N/A * Next execution time for this task in the format returned by
0N/A * System.currentTimeMillis, assuming this task is scheduled for execution.
0N/A * For repeating tasks, this field is updated prior to each task execution.
0N/A */
0N/A long nextExecutionTime;
0N/A
0N/A /**
0N/A * Period in milliseconds for repeating tasks. A positive value indicates
0N/A * fixed-rate execution. A negative value indicates fixed-delay execution.
0N/A * A value of 0 indicates a non-repeating task.
0N/A */
0N/A long period = 0;
0N/A
0N/A /**
0N/A * Creates a new timer task.
0N/A */
0N/A protected TimerTask() {
0N/A }
0N/A
0N/A /**
0N/A * The action to be performed by this timer task.
0N/A */
0N/A public abstract void run();
0N/A
0N/A /**
0N/A * Cancels this timer task. If the task has been scheduled for one-time
0N/A * execution and has not yet run, or has not yet been scheduled, it will
0N/A * never run. If the task has been scheduled for repeated execution, it
0N/A * will never run again. (If the task is running when this call occurs,
0N/A * the task will run to completion, but will never run again.)
0N/A *
0N/A * <p>Note that calling this method from within the <tt>run</tt> method of
0N/A * a repeating timer task absolutely guarantees that the timer task will
0N/A * not run again.
0N/A *
0N/A * <p>This method may be called repeatedly; the second and subsequent
0N/A * calls have no effect.
0N/A *
0N/A * @return true if this task is scheduled for one-time execution and has
0N/A * not yet run, or this task is scheduled for repeated execution.
0N/A * Returns false if the task was scheduled for one-time execution
0N/A * and has already run, or if the task was never scheduled, or if
0N/A * the task was already cancelled. (Loosely speaking, this method
0N/A * returns <tt>true</tt> if it prevents one or more scheduled
0N/A * executions from taking place.)
0N/A */
0N/A public boolean cancel() {
0N/A synchronized(lock) {
0N/A boolean result = (state == SCHEDULED);
0N/A state = CANCELLED;
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the <i>scheduled</i> execution time of the most recent
0N/A * <i>actual</i> execution of this task. (If this method is invoked
0N/A * while task execution is in progress, the return value is the scheduled
0N/A * execution time of the ongoing task execution.)
0N/A *
0N/A * <p>This method is typically invoked from within a task's run method, to
0N/A * determine whether the current execution of the task is sufficiently
0N/A * timely to warrant performing the scheduled activity:
0N/A * <pre>
0N/A * public void run() {
0N/A * if (System.currentTimeMillis() - scheduledExecutionTime() >=
0N/A * MAX_TARDINESS)
0N/A * return; // Too late; skip this execution.
0N/A * // Perform the task
0N/A * }
0N/A * </pre>
0N/A * This method is typically <i>not</i> used in conjunction with
0N/A * <i>fixed-delay execution</i> repeating tasks, as their scheduled
0N/A * execution times are allowed to drift over time, and so are not terribly
0N/A * significant.
0N/A *
0N/A * @return the time at which the most recent execution of this task was
0N/A * scheduled to occur, in the format returned by Date.getTime().
0N/A * The return value is undefined if the task has yet to commence
0N/A * its first execution.
0N/A * @see Date#getTime()
0N/A */
0N/A public long scheduledExecutionTime() {
0N/A synchronized(lock) {
0N/A return (period < 0 ? nextExecutionTime + period
0N/A : nextExecutionTime - period);
0N/A }
0N/A }
0N/A}