OSDN Git Service

3cf24b08b88e6d497b75959b7502d7f102407c61
[pf3gnuchains/gcc-fork.git] / libjava / java / util / TimerTask.java
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.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02111-1307 USA.
20
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
26
27 package java.util;
28
29 /**
30  * Task that can be run at a later time if given to a Timer.
31  * The TimerTask must implement a run method that will be called by the
32  * Timer when the task is scheduled for execution. The task can check when
33  * it should have been scheduled and cancel itself when no longer needed.
34  * <p>
35  * Example:
36  * <code>
37  *  Timer timer = new Timer();
38  *  TimerTask task = new TimerTask() {
39  *      public void run() {
40  *      if (this.scheduledExecutionTime() < System.currentTimeMillis() + 500)
41  *          // Do something
42  *      else
43  *          // Complain: We are more then half a second late!
44  *      if (someStopCondition)
45  *          this.cancel(); // This was our last execution
46  *  };
47  *  timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second
48  * </code>
49  * <p>
50  * Note that a TimerTask object is a one shot object and can only given once
51  * to a Timer. (The Timer will use the TimerTask object for bookkeeping,
52  * in this implementation).
53  * <p>
54  * This class also implements <code>Runnable</code> to make it possible to
55  * give a TimerTask directly as a target to a <code>Thread</code>.
56  *
57  * @see Timer
58  * @since 1.3
59  * @author Mark Wielaard (mark@klomp.org)
60  */
61 public abstract class TimerTask implements Runnable
62 {
63   /**
64    * If positive the next time this task should be run.
65    * If negative this TimerTask is canceled or executed for the last time.
66    */
67   long scheduled;
68
69   /**
70    * If positive the last time this task was run.
71    * If negative this TimerTask has not yet been scheduled.
72    */
73   long lastExecutionTime;
74
75   /**
76    * If positive the number of milliseconds between runs of this task.
77    * If -1 this task doesn't have to be run more then once.
78    */
79   long period;
80
81   /**
82    * If true the next time this task should be run is relative to
83    * the last scheduled time, otherwise it can drift in time.
84    */
85   boolean fixed;
86
87   /**
88    * Creates a TimerTask and marks it as not yet scheduled.
89    */
90   protected TimerTask()
91   {
92     this.scheduled = 0;
93     this.lastExecutionTime = -1;
94   }
95
96   /**
97    * Marks the task as canceled and prevents any further execution.
98    * Returns true if the task was scheduled for any execution in the future
99    * and this cancel operation prevents that execution from happening.
100    * <p>
101    * A task that has been canceled can never be scheduled again.
102    * <p>
103    * In this implementation the TimerTask it is possible that the Timer does
104    * keep a reference to the TimerTask until the first time the TimerTask
105    * is actually scheduled. But the reference will disappear immediatly when
106    * cancel is called from within the TimerTask run method.
107    */
108   public boolean cancel()
109   {
110     boolean prevented_execution = (this.scheduled >= 0);
111     this.scheduled = -1;
112     return prevented_execution;
113   }
114
115   /**
116    * Method that is called when this task is scheduled for execution.
117    */
118   public abstract void run();
119
120   /**
121    * Returns the last time this task was scheduled or (when called by the
122    * task from the run method) the time the current execution of the task
123    * was scheduled. When the task has not yet run the return value is
124    * undefined.
125    * <p>
126    * Can be used (when the task is scheduled at fixed rate) to see the
127    * difference between the requested schedule time and the actual time
128    * that can be found with <code>System.currentTimeMillis()</code>.
129    */
130   public long scheduledExecutionTime()
131   {
132     return lastExecutionTime;
133   }
134 }