001 /* TimerTask.java -- Task that can be run at a later time if given to a Timer.
002 Copyright (C) 2000 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038 package java.util;
039
040 /**
041 * Task that can be run at a later time if given to a Timer.
042 * The TimerTask must implement a run method that will be called by the
043 * Timer when the task is scheduled for execution. The task can check when
044 * it should have been scheduled and cancel itself when no longer needed.
045 * <p>
046 * Example:
047 * <pre>
048 * Timer timer = new Timer();
049 * TimerTask task = new TimerTask() {
050 * public void run() {
051 * if (this.scheduledExecutionTime() < System.currentTimeMillis() + 500)
052 * // Do something
053 * else
054 * // Complain: We are more then half a second late!
055 * if (someStopCondition)
056 * this.cancel(); // This was our last execution
057 * };
058 * timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second
059 * </pre>
060 * <p>
061 * Note that a TimerTask object is a one shot object and can only given once
062 * to a Timer. (The Timer will use the TimerTask object for bookkeeping,
063 * in this implementation).
064 * <p>
065 * This class also implements <code>Runnable</code> to make it possible to
066 * give a TimerTask directly as a target to a <code>Thread</code>.
067 *
068 * @see Timer
069 * @since 1.3
070 * @author Mark Wielaard (mark@klomp.org)
071 */
072 public abstract class TimerTask implements Runnable
073 {
074 /**
075 * If positive the next time this task should be run.
076 * If negative this TimerTask is canceled or executed for the last time.
077 */
078 long scheduled;
079
080 /**
081 * If positive the last time this task was run.
082 * If negative this TimerTask has not yet been scheduled.
083 */
084 long lastExecutionTime;
085
086 /**
087 * If positive the number of milliseconds between runs of this task.
088 * If -1 this task doesn't have to be run more then once.
089 */
090 long period;
091
092 /**
093 * If true the next time this task should be run is relative to
094 * the last scheduled time, otherwise it can drift in time.
095 */
096 boolean fixed;
097
098 /**
099 * Creates a TimerTask and marks it as not yet scheduled.
100 */
101 protected TimerTask()
102 {
103 this.scheduled = 0;
104 this.lastExecutionTime = -1;
105 }
106
107 /**
108 * Marks the task as canceled and prevents any further execution.
109 * Returns true if the task was scheduled for any execution in the future
110 * and this cancel operation prevents that execution from happening.
111 * <p>
112 * A task that has been canceled can never be scheduled again.
113 * <p>
114 * In this implementation the TimerTask it is possible that the Timer does
115 * keep a reference to the TimerTask until the first time the TimerTask
116 * is actually scheduled. But the reference will disappear immediatly when
117 * cancel is called from within the TimerTask run method.
118 */
119 public boolean cancel()
120 {
121 boolean prevented_execution = (this.scheduled >= 0);
122 this.scheduled = -1;
123 return prevented_execution;
124 }
125
126 /**
127 * Method that is called when this task is scheduled for execution.
128 */
129 public abstract void run();
130
131 /**
132 * Returns the last time this task was scheduled or (when called by the
133 * task from the run method) the time the current execution of the task
134 * was scheduled. When the task has not yet run the return value is
135 * undefined.
136 * <p>
137 * Can be used (when the task is scheduled at fixed rate) to see the
138 * difference between the requested schedule time and the actual time
139 * that can be found with <code>System.currentTimeMillis()</code>.
140 */
141 public long scheduledExecutionTime()
142 {
143 return lastExecutionTime;
144 }
145 }