rt/emul/compact/src/main/java/java/util/TimerTask.java
branchjdk7-b147
changeset 1405 f8f4cf9046fd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/TimerTask.java	Sat Nov 02 16:40:37 2013 +0100
     1.3 @@ -0,0 +1,158 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999, 2004, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util;
    1.30 +
    1.31 +/**
    1.32 + * A task that can be scheduled for one-time or repeated execution by a Timer.
    1.33 + *
    1.34 + * @author  Josh Bloch
    1.35 + * @see     Timer
    1.36 + * @since   1.3
    1.37 + */
    1.38 +
    1.39 +public abstract class TimerTask implements Runnable {
    1.40 +    /**
    1.41 +     * This object is used to control access to the TimerTask internals.
    1.42 +     */
    1.43 +    final Object lock = new Object();
    1.44 +
    1.45 +    /**
    1.46 +     * The state of this task, chosen from the constants below.
    1.47 +     */
    1.48 +    int state = VIRGIN;
    1.49 +
    1.50 +    /**
    1.51 +     * This task has not yet been scheduled.
    1.52 +     */
    1.53 +    static final int VIRGIN = 0;
    1.54 +
    1.55 +    /**
    1.56 +     * This task is scheduled for execution.  If it is a non-repeating task,
    1.57 +     * it has not yet been executed.
    1.58 +     */
    1.59 +    static final int SCHEDULED   = 1;
    1.60 +
    1.61 +    /**
    1.62 +     * This non-repeating task has already executed (or is currently
    1.63 +     * executing) and has not been cancelled.
    1.64 +     */
    1.65 +    static final int EXECUTED    = 2;
    1.66 +
    1.67 +    /**
    1.68 +     * This task has been cancelled (with a call to TimerTask.cancel).
    1.69 +     */
    1.70 +    static final int CANCELLED   = 3;
    1.71 +
    1.72 +    /**
    1.73 +     * Next execution time for this task in the format returned by
    1.74 +     * System.currentTimeMillis, assuming this task is scheduled for execution.
    1.75 +     * For repeating tasks, this field is updated prior to each task execution.
    1.76 +     */
    1.77 +    long nextExecutionTime;
    1.78 +
    1.79 +    /**
    1.80 +     * Period in milliseconds for repeating tasks.  A positive value indicates
    1.81 +     * fixed-rate execution.  A negative value indicates fixed-delay execution.
    1.82 +     * A value of 0 indicates a non-repeating task.
    1.83 +     */
    1.84 +    long period = 0;
    1.85 +
    1.86 +    /**
    1.87 +     * Creates a new timer task.
    1.88 +     */
    1.89 +    protected TimerTask() {
    1.90 +    }
    1.91 +
    1.92 +    /**
    1.93 +     * The action to be performed by this timer task.
    1.94 +     */
    1.95 +    public abstract void run();
    1.96 +
    1.97 +    /**
    1.98 +     * Cancels this timer task.  If the task has been scheduled for one-time
    1.99 +     * execution and has not yet run, or has not yet been scheduled, it will
   1.100 +     * never run.  If the task has been scheduled for repeated execution, it
   1.101 +     * will never run again.  (If the task is running when this call occurs,
   1.102 +     * the task will run to completion, but will never run again.)
   1.103 +     *
   1.104 +     * <p>Note that calling this method from within the <tt>run</tt> method of
   1.105 +     * a repeating timer task absolutely guarantees that the timer task will
   1.106 +     * not run again.
   1.107 +     *
   1.108 +     * <p>This method may be called repeatedly; the second and subsequent
   1.109 +     * calls have no effect.
   1.110 +     *
   1.111 +     * @return true if this task is scheduled for one-time execution and has
   1.112 +     *         not yet run, or this task is scheduled for repeated execution.
   1.113 +     *         Returns false if the task was scheduled for one-time execution
   1.114 +     *         and has already run, or if the task was never scheduled, or if
   1.115 +     *         the task was already cancelled.  (Loosely speaking, this method
   1.116 +     *         returns <tt>true</tt> if it prevents one or more scheduled
   1.117 +     *         executions from taking place.)
   1.118 +     */
   1.119 +    public boolean cancel() {
   1.120 +        synchronized(lock) {
   1.121 +            boolean result = (state == SCHEDULED);
   1.122 +            state = CANCELLED;
   1.123 +            return result;
   1.124 +        }
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Returns the <i>scheduled</i> execution time of the most recent
   1.129 +     * <i>actual</i> execution of this task.  (If this method is invoked
   1.130 +     * while task execution is in progress, the return value is the scheduled
   1.131 +     * execution time of the ongoing task execution.)
   1.132 +     *
   1.133 +     * <p>This method is typically invoked from within a task's run method, to
   1.134 +     * determine whether the current execution of the task is sufficiently
   1.135 +     * timely to warrant performing the scheduled activity:
   1.136 +     * <pre>
   1.137 +     *   public void run() {
   1.138 +     *       if (System.currentTimeMillis() - scheduledExecutionTime() >=
   1.139 +     *           MAX_TARDINESS)
   1.140 +     *               return;  // Too late; skip this execution.
   1.141 +     *       // Perform the task
   1.142 +     *   }
   1.143 +     * </pre>
   1.144 +     * This method is typically <i>not</i> used in conjunction with
   1.145 +     * <i>fixed-delay execution</i> repeating tasks, as their scheduled
   1.146 +     * execution times are allowed to drift over time, and so are not terribly
   1.147 +     * significant.
   1.148 +     *
   1.149 +     * @return the time at which the most recent execution of this task was
   1.150 +     *         scheduled to occur, in the format returned by Date.getTime().
   1.151 +     *         The return value is undefined if the task has yet to commence
   1.152 +     *         its first execution.
   1.153 +     * @see Date#getTime()
   1.154 +     */
   1.155 +    public long scheduledExecutionTime() {
   1.156 +        synchronized(lock) {
   1.157 +            return (period < 0 ? nextExecutionTime + period
   1.158 +                               : nextExecutionTime - period);
   1.159 +        }
   1.160 +    }
   1.161 +}