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 | 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
|
---|
24 | combination.
|
---|
25 |
|
---|
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. */
|
---|
37 |
|
---|
38 | package java.util;
|
---|
39 |
|
---|
40 | /**
|
---|
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.
|
---|
45 | * <p>
|
---|
46 | * Example:
|
---|
47 | * <pre>
|
---|
48 | * Timer timer = new Timer();
|
---|
49 | * TimerTask task = new TimerTask() {
|
---|
50 | * public void run() {
|
---|
51 | * if (this.scheduledExecutionTime() < System.currentTimeMillis() + 500)
|
---|
52 | * // Do something
|
---|
53 | * else
|
---|
54 | * // Complain: We are more then half a second late!
|
---|
55 | * if (someStopCondition)
|
---|
56 | * this.cancel(); // This was our last execution
|
---|
57 | * };
|
---|
58 | * timer.scheduleAtFixedRate(task, 1000, 1000); // schedule every second
|
---|
59 | * </pre>
|
---|
60 | * <p>
|
---|
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).
|
---|
64 | * <p>
|
---|
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>.
|
---|
67 | *
|
---|
68 | * @see Timer
|
---|
69 | * @since 1.3
|
---|
70 | * @author Mark Wielaard (mark@klomp.org)
|
---|
71 | */
|
---|
72 | public abstract class TimerTask implements Runnable
|
---|
73 | {
|
---|
74 | /**
|
---|
75 | * If positive the next time this task should be run.
|
---|
76 | * If negative this TimerTask is canceled or executed for the last time.
|
---|
77 | */
|
---|
78 | long scheduled;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * If positive the last time this task was run.
|
---|
82 | * If negative this TimerTask has not yet been scheduled.
|
---|
83 | */
|
---|
84 | long lastExecutionTime;
|
---|
85 |
|
---|
86 | /**
|
---|
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.
|
---|
89 | */
|
---|
90 | long period;
|
---|
91 |
|
---|
92 | /**
|
---|
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.
|
---|
95 | */
|
---|
96 | boolean fixed;
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * 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 | }
|
---|