1 /* TimerTask.java -- Task that can be run at a later time if given to a Timer.
2 Copyright (C) 2000 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 * Task that can be run at a later time if given to a Timer.
42 * The TimerTask must implement a run method that will be called by the
43 * Timer when the task is scheduled for execution. The task can check when
44 * it should have been scheduled and cancel itself when no longer needed.
48 * Timer timer = new Timer();
49 * TimerTask task = new TimerTask() {
51 * if (this.scheduledExecutionTime() < System.currentTimeMillis() + 500)
54 * // Complain: We are more then half a second late!
55 * if (someStopCondition)
56 * this.cancel(); // This was our last execution
58 * timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second
61 * Note that a TimerTask object is a one shot object and can only given once
62 * to a Timer. (The Timer will use the TimerTask object for bookkeeping,
63 * in this implementation).
65 * This class also implements <code>Runnable</code> to make it possible to
66 * give a TimerTask directly as a target to a <code>Thread</code>.
70 * @author Mark Wielaard (mark@klomp.org)
72 public abstract class TimerTask
implements Runnable
75 * If positive the next time this task should be run.
76 * If negative this TimerTask is canceled or executed for the last time.
81 * If positive the last time this task was run.
82 * If negative this TimerTask has not yet been scheduled.
84 long lastExecutionTime
;
87 * If positive the number of milliseconds between runs of this task.
88 * If -1 this task doesn't have to be run more then once.
93 * If true the next time this task should be run is relative to
94 * the last scheduled time, otherwise it can drift in time.
99 * Creates a TimerTask and marks it as not yet scheduled.
101 protected TimerTask()
104 this.lastExecutionTime
= -1;
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.
112 * A task that has been canceled can never be scheduled again.
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.
119 public boolean cancel()
121 boolean prevented_execution
= (this.scheduled
>= 0);
123 return prevented_execution
;
127 * Method that is called when this task is scheduled for execution.
129 public abstract void run();
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
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>.
141 public long scheduledExecutionTime()
143 return lastExecutionTime
;