jdk/src/share/classes/java/util/TimerTask.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A task that can be scheduled for one-time or repeated execution by a Timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @see     Timer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public abstract class TimerTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * This object is used to control access to the TimerTask internals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * The state of this task, chosen from the constants below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int state = VIRGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * This task has not yet been scheduled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static final int VIRGIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * This task is scheduled for execution.  If it is a non-repeating task,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * it has not yet been executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static final int SCHEDULED   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * This non-repeating task has already executed (or is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * executing) and has not been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static final int EXECUTED    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * This task has been cancelled (with a call to TimerTask.cancel).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    static final int CANCELLED   = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Next execution time for this task in the format returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * System.currentTimeMillis, assuming this task is scheduled for execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * For repeating tasks, this field is updated prior to each task execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    long nextExecutionTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Period in milliseconds for repeating tasks.  A positive value indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * fixed-rate execution.  A negative value indicates fixed-delay execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * A value of 0 indicates a non-repeating task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    long period = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Creates a new timer task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected TimerTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The action to be performed by this timer task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public abstract void run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Cancels this timer task.  If the task has been scheduled for one-time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * execution and has not yet run, or has not yet been scheduled, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * never run.  If the task has been scheduled for repeated execution, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * will never run again.  (If the task is running when this call occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the task will run to completion, but will never run again.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * <p>Note that calling this method from within the <tt>run</tt> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * a repeating timer task absolutely guarantees that the timer task will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * not run again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>This method may be called repeatedly; the second and subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * calls have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return true if this task is scheduled for one-time execution and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *         not yet run, or this task is scheduled for repeated execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *         Returns false if the task was scheduled for one-time execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *         and has already run, or if the task was never scheduled, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *         the task was already cancelled.  (Loosely speaking, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *         returns <tt>true</tt> if it prevents one or more scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *         executions from taking place.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public boolean cancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            boolean result = (state == SCHEDULED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            state = CANCELLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Returns the <i>scheduled</i> execution time of the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <i>actual</i> execution of this task.  (If this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * while task execution is in progress, the return value is the scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * execution time of the ongoing task execution.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>This method is typically invoked from within a task's run method, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * determine whether the current execution of the task is sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * timely to warrant performing the scheduled activity:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *       if (System.currentTimeMillis() - scheduledExecutionTime() >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *           MAX_TARDINESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *               return;  // Too late; skip this execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *       // Perform the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * This method is typically <i>not</i> used in conjunction with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <i>fixed-delay execution</i> repeating tasks, as their scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * execution times are allowed to drift over time, and so are not terribly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the time at which the most recent execution of this task was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *         scheduled to occur, in the format returned by Date.getTime().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *         The return value is undefined if the task has yet to commence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *         its first execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see Date#getTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public long scheduledExecutionTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return (period < 0 ? nextExecutionTime + period
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                               : nextExecutionTime - period);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}