src/share/classes/java/util/TimerTask.java

Print this page




 113      *         returns <tt>true</tt> if it prevents one or more scheduled
 114      *         executions from taking place.)
 115      */
 116     public boolean cancel() {
 117         synchronized(lock) {
 118             boolean result = (state == SCHEDULED);
 119             state = CANCELLED;
 120             return result;
 121         }
 122     }
 123 
 124     /**
 125      * Returns the <i>scheduled</i> execution time of the most recent
 126      * <i>actual</i> execution of this task.  (If this method is invoked
 127      * while task execution is in progress, the return value is the scheduled
 128      * execution time of the ongoing task execution.)
 129      *
 130      * <p>This method is typically invoked from within a task's run method, to
 131      * determine whether the current execution of the task is sufficiently
 132      * timely to warrant performing the scheduled activity:
 133      * <pre>
 134      *   public void run() {
 135      *       if (System.currentTimeMillis() - scheduledExecutionTime() >=
 136      *           MAX_TARDINESS)
 137      *               return;  // Too late; skip this execution.
 138      *       // Perform the task
 139      *   }
 140      * </pre>
 141      * This method is typically <i>not</i> used in conjunction with
 142      * <i>fixed-delay execution</i> repeating tasks, as their scheduled
 143      * execution times are allowed to drift over time, and so are not terribly
 144      * significant.
 145      *
 146      * @return the time at which the most recent execution of this task was
 147      *         scheduled to occur, in the format returned by Date.getTime().
 148      *         The return value is undefined if the task has yet to commence
 149      *         its first execution.
 150      * @see Date#getTime()
 151      */
 152     public long scheduledExecutionTime() {
 153         synchronized(lock) {
 154             return (period < 0 ? nextExecutionTime + period
 155                                : nextExecutionTime - period);
 156         }
 157     }
 158 }


 113      *         returns <tt>true</tt> if it prevents one or more scheduled
 114      *         executions from taking place.)
 115      */
 116     public boolean cancel() {
 117         synchronized(lock) {
 118             boolean result = (state == SCHEDULED);
 119             state = CANCELLED;
 120             return result;
 121         }
 122     }
 123 
 124     /**
 125      * Returns the <i>scheduled</i> execution time of the most recent
 126      * <i>actual</i> execution of this task.  (If this method is invoked
 127      * while task execution is in progress, the return value is the scheduled
 128      * execution time of the ongoing task execution.)
 129      *
 130      * <p>This method is typically invoked from within a task's run method, to
 131      * determine whether the current execution of the task is sufficiently
 132      * timely to warrant performing the scheduled activity:
 133      * <pre>{@code
 134      *   public void run() {
 135      *       if (System.currentTimeMillis() - scheduledExecutionTime() >=
 136      *           MAX_TARDINESS)
 137      *               return;  // Too late; skip this execution.
 138      *       // Perform the task
 139      *   }
 140      * }</pre>
 141      * This method is typically <i>not</i> used in conjunction with
 142      * <i>fixed-delay execution</i> repeating tasks, as their scheduled
 143      * execution times are allowed to drift over time, and so are not terribly
 144      * significant.
 145      *
 146      * @return the time at which the most recent execution of this task was
 147      *         scheduled to occur, in the format returned by Date.getTime().
 148      *         The return value is undefined if the task has yet to commence
 149      *         its first execution.
 150      * @see Date#getTime()
 151      */
 152     public long scheduledExecutionTime() {
 153         synchronized(lock) {
 154             return (period < 0 ? nextExecutionTime + period
 155                                : nextExecutionTime - period);
 156         }
 157     }
 158 }