1 *java.util.Timer* *Timer* A facility for threads to schedule tasks for future ex
4 extends |java.lang.Object|
6 |java.util.Timer_Description|
7 |java.util.Timer_Fields|
8 |java.util.Timer_Constructors|
9 |java.util.Timer_Methods|
11 ================================================================================
13 *java.util.Timer_Constructors*
14 |java.util.Timer()|Creates a new timer.
15 |java.util.Timer(boolean)|Creates a new timer whose associated thread may be sp
16 |java.util.Timer(String)|Creates a new timer whose associated thread has the sp
17 |java.util.Timer(String,boolean)|Creates a new timer whose associated thread ha
19 *java.util.Timer_Methods*
20 |java.util.Timer.cancel()|Terminates this timer, discarding any currently sched
21 |java.util.Timer.purge()|Removes all cancelled tasks from this timer's task que
22 |java.util.Timer.schedule(TimerTask,Date)|Schedules the specified task for exec
23 |java.util.Timer.schedule(TimerTask,Date,long)|Schedules the specified task for
24 |java.util.Timer.schedule(TimerTask,long)|Schedules the specified task for exec
25 |java.util.Timer.schedule(TimerTask,long,long)|Schedules the specified task for
26 |java.util.Timer.scheduleAtFixedRate(TimerTask,Date,long)|Schedules the specifi
27 |java.util.Timer.scheduleAtFixedRate(TimerTask,long,long)|Schedules the specifi
29 *java.util.Timer_Description*
31 A facility for threads to schedule tasks for future execution in a background
32 thread. Tasks may be scheduled for one-time execution, or for repeated
33 execution at regular intervals.
35 Corresponding to each Timer object is a single background thread that is used
36 to execute all of the timer's tasks, sequentially. Timer tasks should complete
37 quickly. If a timer task takes excessive time to complete, it "hogs" the
38 timer's task execution thread. This can, in turn, delay the execution of
39 subsequent tasks, which may "bunch up" and execute in rapid succession when
40 (and if) the offending task finally completes.
42 After the last live reference to a Timer object goes away and all outstanding
43 tasks have completed execution, the timer's task execution thread terminates
44 gracefully (and becomes subject to garbage collection). However, this can take
45 arbitrarily long to occur. By default, the task execution thread does not run
46 as a daemon thread, so it is capable of keeping an application from
47 terminating. If a caller wants to terminate a timer's task execution thread
48 rapidly, the caller should invoke the timer's cancel method.
50 If the timer's task execution thread terminates unexpectedly, for example,
51 because its stop method is invoked, any further attempt to schedule a task on
52 the timer will result in an IllegalStateException, as if the timer's cancel
53 method had been invoked.
55 This class is thread-safe: multiple threads can share a single Timer object
56 without the need for external synchronization.
58 This class does not offer real-time guarantees: it schedules tasks using the
59 Object.wait(long) method.
61 Implementation note: This class scales to large numbers of concurrently
62 scheduled tasks (thousands should present no problem). Internally, it uses a
63 binary heap to represent its task queue, so the cost to schedule a task is
64 O(log n), where n is the number of concurrently scheduled tasks.
66 Implementation note: All constructors start a timer thread.
74 Creates a new timer. The associated thread does not run as a daemon.
77 *java.util.Timer(boolean)*
79 public Timer(boolean isDaemon)
81 Creates a new timer whose associated thread may be specified to run as a
82 daemon. A daemon thread is called for if the timer will be used to schedule
83 repeating "maintenance activities", which must be performed as long as the
84 application is running, but should not prolong the lifetime of the application.
86 isDaemon - true if the associated thread should run as a daemon.
88 *java.util.Timer(String)*
90 public Timer(java.lang.String name)
92 Creates a new timer whose associated thread has the specified name. The
93 associated thread does not run as a daemon.
95 name - the name of the associated thread
97 *java.util.Timer(String,boolean)*
100 java.lang.String name,
103 Creates a new timer whose associated thread has the specified name, and may be
104 specified to run as a daemon.
106 name - the name of the associated thread
107 isDaemon - true if the associated thread should run as a daemon
109 *java.util.Timer.cancel()*
113 Terminates this timer, discarding any currently scheduled tasks. Does not
114 interfere with a currently executing task (if it exists). Once a timer has been
115 terminated, its execution thread terminates gracefully, and no more tasks may
118 Note that calling this method from within the run method of a timer task that
119 was invoked by this timer absolutely guarantees that the ongoing task execution
120 is the last task execution that will ever be performed by this timer.
122 This method may be called repeatedly; the second and subsequent calls have no
127 *java.util.Timer.purge()*
131 Removes all cancelled tasks from this timer's task queue. Calling this method
132 has no effect on the behavior of the timer, but eliminates the references to
133 the cancelled tasks from the queue. If there are no external references to
134 these tasks, they become eligible for garbage collection.
136 Most programs will have no need to call this method. It is designed for use by
137 the rare application that cancels a large number of tasks. Calling this method
138 trades time for space: the runtime of the method may be proportional to n + c
139 log n, where n is the number of tasks in the queue and c is the number of
142 Note that it is permissible to call this method from within a a task scheduled
147 Returns: the number of tasks removed from the queue.
149 *java.util.Timer.schedule(TimerTask,Date)*
151 public void schedule(
152 java.util.TimerTask task,
155 Schedules the specified task for execution at the specified time. If the time
156 is in the past, the task is scheduled for immediate execution.
159 task - task to be scheduled.
160 time - time at which task is to be executed.
162 *java.util.Timer.schedule(TimerTask,Date,long)*
164 public void schedule(
165 java.util.TimerTask task,
166 java.util.Date firstTime,
169 Schedules the specified task for repeated fixed-delay execution, beginning at
170 the specified time. Subsequent executions take place at approximately regular
171 intervals, separated by the specified period.
173 In fixed-delay execution, each execution is scheduled relative to the actual
174 execution time of the previous execution. If an execution is delayed for any
175 reason (such as garbage collection or other background activity), subsequent
176 executions will be delayed as well. In the long run, the frequency of execution
177 will generally be slightly lower than the reciprocal of the specified period
178 (assuming the system clock underlying Object.wait(long) is accurate).
180 Fixed-delay execution is appropriate for recurring activities that require
181 "smoothness." In other words, it is appropriate for activities where it is more
182 important to keep the frequency accurate in the short run than in the long run.
183 This includes most animation tasks, such as blinking a cursor at regular
184 intervals. It also includes tasks wherein regular activity is performed in
185 response to human input, such as automatically repeating a character as long as
189 task - task to be scheduled.
190 firstTime - First time at which task is to be executed.
191 period - time in milliseconds between successive task executions.
193 *java.util.Timer.schedule(TimerTask,long)*
195 public void schedule(
196 java.util.TimerTask task,
199 Schedules the specified task for execution after the specified delay.
202 task - task to be scheduled.
203 delay - delay in milliseconds before task is to be executed.
205 *java.util.Timer.schedule(TimerTask,long,long)*
207 public void schedule(
208 java.util.TimerTask task,
212 Schedules the specified task for repeated fixed-delay execution, beginning
213 after the specified delay. Subsequent executions take place at approximately
214 regular intervals separated by the specified period.
216 In fixed-delay execution, each execution is scheduled relative to the actual
217 execution time of the previous execution. If an execution is delayed for any
218 reason (such as garbage collection or other background activity), subsequent
219 executions will be delayed as well. In the long run, the frequency of execution
220 will generally be slightly lower than the reciprocal of the specified period
221 (assuming the system clock underlying Object.wait(long) is accurate).
223 Fixed-delay execution is appropriate for recurring activities that require
224 "smoothness." In other words, it is appropriate for activities where it is more
225 important to keep the frequency accurate in the short run than in the long run.
226 This includes most animation tasks, such as blinking a cursor at regular
227 intervals. It also includes tasks wherein regular activity is performed in
228 response to human input, such as automatically repeating a character as long as
232 task - task to be scheduled.
233 delay - delay in milliseconds before task is to be executed.
234 period - time in milliseconds between successive task executions.
236 *java.util.Timer.scheduleAtFixedRate(TimerTask,Date,long)*
238 public void scheduleAtFixedRate(
239 java.util.TimerTask task,
240 java.util.Date firstTime,
243 Schedules the specified task for repeated fixed-rate execution, beginning at
244 the specified time. Subsequent executions take place at approximately regular
245 intervals, separated by the specified period.
247 In fixed-rate execution, each execution is scheduled relative to the scheduled
248 execution time of the initial execution. If an execution is delayed for any
249 reason (such as garbage collection or other background activity), two or more
250 executions will occur in rapid succession to "catch up." In the long run, the
251 frequency of execution will be exactly the reciprocal of the specified period
252 (assuming the system clock underlying Object.wait(long) is accurate).
254 Fixed-rate execution is appropriate for recurring activities that are sensitive
255 to absolute time, such as ringing a chime every hour on the hour, or running
256 scheduled maintenance every day at a particular time. It is also appropriate
257 for recurring activities where the total time to perform a fixed number of
258 executions is important, such as a countdown timer that ticks once every second
259 for ten seconds. Finally, fixed-rate execution is appropriate for scheduling
260 multiple repeating timer tasks that must remain synchronized with respect to
264 task - task to be scheduled.
265 firstTime - First time at which task is to be executed.
266 period - time in milliseconds between successive task executions.
268 *java.util.Timer.scheduleAtFixedRate(TimerTask,long,long)*
270 public void scheduleAtFixedRate(
271 java.util.TimerTask task,
275 Schedules the specified task for repeated fixed-rate execution, beginning after
276 the specified delay. Subsequent executions take place at approximately regular
277 intervals, separated by the specified period.
279 In fixed-rate execution, each execution is scheduled relative to the scheduled
280 execution time of the initial execution. If an execution is delayed for any
281 reason (such as garbage collection or other background activity), two or more
282 executions will occur in rapid succession to "catch up." In the long run, the
283 frequency of execution will be exactly the reciprocal of the specified period
284 (assuming the system clock underlying Object.wait(long) is accurate).
286 Fixed-rate execution is appropriate for recurring activities that are sensitive
287 to absolute time, such as ringing a chime every hour on the hour, or running
288 scheduled maintenance every day at a particular time. It is also appropriate
289 for recurring activities where the total time to perform a fixed number of
290 executions is important, such as a countdown timer that ticks once every second
291 for ten seconds. Finally, fixed-rate execution is appropriate for scheduling
292 multiple repeating timer tasks that must remain synchronized with respect to
296 task - task to be scheduled.
297 delay - delay in milliseconds before task is to be executed.
298 period - time in milliseconds between successive task executions.