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