jdk/src/java.base/share/classes/java/util/TimerTask.java
author chegar
Tue, 07 Apr 2015 10:33:08 +0100
changeset 29816 43ad6bf3975b
parent 26715 5e166a2dc3ac
child 32108 aa5490a167ee
permissions -rw-r--r--
8076442: Cannot fully read BitSet.stream() if bit Integer.MAX_VALUE is set Reviewed-by: alanb, henryjen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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
/**
26715
5e166a2dc3ac 8058550: Clarify that TimerTasks are not reusable
martin
parents: 25859
diff changeset
    29
 * A task that can be scheduled for one-time or repeated execution by a
5e166a2dc3ac 8058550: Clarify that TimerTasks are not reusable
martin
parents: 25859
diff changeset
    30
 * {@link Timer}.
5e166a2dc3ac 8058550: Clarify that TimerTasks are not reusable
martin
parents: 25859
diff changeset
    31
 *
5e166a2dc3ac 8058550: Clarify that TimerTasks are not reusable
martin
parents: 25859
diff changeset
    32
 * <p>A timer task is <em>not</em> reusable.  Once a task has been scheduled
5e166a2dc3ac 8058550: Clarify that TimerTasks are not reusable
martin
parents: 25859
diff changeset
    33
 * for execution on a {@code Timer} or cancelled, subsequent attempts to
5e166a2dc3ac 8058550: Clarify that TimerTasks are not reusable
martin
parents: 25859
diff changeset
    34
 * schedule it for execution will throw {@code IllegalStateException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public abstract class TimerTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * This object is used to control access to the TimerTask internals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * The state of this task, chosen from the constants below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int state = VIRGIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * This task has not yet been scheduled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static final int VIRGIN = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * This task is scheduled for execution.  If it is a non-repeating task,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * it has not yet been executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static final int SCHEDULED   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * This non-repeating task has already executed (or is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * executing) and has not been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static final int EXECUTED    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * This task has been cancelled (with a call to TimerTask.cancel).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static final int CANCELLED   = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Next execution time for this task in the format returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * System.currentTimeMillis, assuming this task is scheduled for execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * For repeating tasks, this field is updated prior to each task execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    long nextExecutionTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Period in milliseconds for repeating tasks.  A positive value indicates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * fixed-rate execution.  A negative value indicates fixed-delay execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * A value of 0 indicates a non-repeating task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    long period = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Creates a new timer task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected TimerTask() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The action to be performed by this timer task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public abstract void run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Cancels this timer task.  If the task has been scheduled for one-time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * execution and has not yet run, or has not yet been scheduled, it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * never run.  If the task has been scheduled for repeated execution, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * will never run again.  (If the task is running when this call occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the task will run to completion, but will never run again.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>Note that calling this method from within the <tt>run</tt> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * a repeating timer task absolutely guarantees that the timer task will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * not run again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>This method may be called repeatedly; the second and subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * calls have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @return true if this task is scheduled for one-time execution and has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *         not yet run, or this task is scheduled for repeated execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *         Returns false if the task was scheduled for one-time execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *         and has already run, or if the task was never scheduled, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *         the task was already cancelled.  (Loosely speaking, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *         returns <tt>true</tt> if it prevents one or more scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *         executions from taking place.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public boolean cancel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            boolean result = (state == SCHEDULED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            state = CANCELLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns the <i>scheduled</i> execution time of the most recent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <i>actual</i> execution of this task.  (If this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * while task execution is in progress, the return value is the scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * execution time of the ongoing task execution.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <p>This method is typically invoked from within a task's run method, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * determine whether the current execution of the task is sufficiently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * timely to warrant performing the scheduled activity:
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   137
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *       if (System.currentTimeMillis() - scheduledExecutionTime() >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *           MAX_TARDINESS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *               return;  // Too late; skip this execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *       // Perform the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *   }
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   144
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * This method is typically <i>not</i> used in conjunction with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <i>fixed-delay execution</i> repeating tasks, as their scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * execution times are allowed to drift over time, and so are not terribly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return the time at which the most recent execution of this task was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *         scheduled to occur, in the format returned by Date.getTime().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *         The return value is undefined if the task has yet to commence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         its first execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see Date#getTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public long scheduledExecutionTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            return (period < 0 ? nextExecutionTime + period
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                               : nextExecutionTime - period);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}