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.
73 Creates a new timer. The associated thread does not run as a daemon.
76 *java.util.Timer(boolean)*
78 public Timer(boolean isDaemon)
80 Creates a new timer whose associated thread may be specified to run as a
81 daemon. A daemon thread is called for if the timer will be used to schedule
82 repeating "maintenance activities", which must be performed as long as the
83 application is running, but should not prolong the lifetime of the application.
85 isDaemon - true if the associated thread should run as a daemon.
87 *java.util.Timer(String)*
89 public Timer(java.lang.String name)
91 Creates a new timer whose associated thread has the specified name. The
92 associated thread does not run as a daemon.
94 name - the name of the associated thread
96 *java.util.Timer(String,boolean)*
99 java.lang.String name,
102 Creates a new timer whose associated thread has the specified name, and may be
103 specified to run as a daemon.
105 name - the name of the associated thread
106 isDaemon - true if the associated thread should run as a daemon
108 *java.util.Timer.cancel()*
112 Terminates this timer, discarding any currently scheduled tasks. Does not
113 interfere with a currently executing task (if it exists). Once a timer has been
114 terminated, its execution thread terminates gracefully, and no more tasks may
117 Note that calling this method from within the run method of a timer task that
118 was invoked by this timer absolutely guarantees that the ongoing task execution
119 is the last task execution that will ever be performed by this timer.
121 This method may be called repeatedly; the second and subsequent calls have no
125 *java.util.Timer.purge()*
129 Removes all cancelled tasks from this timer's task queue. Calling this method
130 has no effect on the behavior of the timer, but eliminates the references to
131 the cancelled tasks from the queue. If there are no external references to
132 these tasks, they become eligible for garbage collection.
134 Most programs will have no need to call this method. It is designed for use by
135 the rare application that cancels a large number of tasks. Calling this method
136 trades time for space: the runtime of the method may be proportional to n + c
137 log n, where n is the number of tasks in the queue and c is the number of
140 Note that it is permissible to call this method from within a a task scheduled
144 Returns: the number of tasks removed from the queue.
145 *java.util.Timer.schedule(TimerTask,Date)*
147 public void schedule(
148 java.util.TimerTask task,
151 Schedules the specified task for execution at the specified time. If the time
152 is in the past, the task is scheduled for immediate execution.
154 task - task to be scheduled.
155 time - time at which task is to be executed.
157 *java.util.Timer.schedule(TimerTask,Date,long)*
159 public void schedule(
160 java.util.TimerTask task,
161 java.util.Date firstTime,
164 Schedules the specified task for repeated fixed-delay execution, beginning at
165 the specified time. Subsequent executions take place at approximately regular
166 intervals, separated by the specified period.
168 In fixed-delay execution, each execution is scheduled relative to the actual
169 execution time of the previous execution. If an execution is delayed for any
170 reason (such as garbage collection or other background activity), subsequent
171 executions will be delayed as well. In the long run, the frequency of execution
172 will generally be slightly lower than the reciprocal of the specified period
173 (assuming the system clock underlying Object.wait(long) is accurate).
175 Fixed-delay execution is appropriate for recurring activities that require
176 "smoothness." In other words, it is appropriate for activities where it is more
177 important to keep the frequency accurate in the short run than in the long run.
178 This includes most animation tasks, such as blinking a cursor at regular
179 intervals. It also includes tasks wherein regular activity is performed in
180 response to human input, such as automatically repeating a character as long as
183 task - task to be scheduled.
184 firstTime - First time at which task is to be executed.
185 period - time in milliseconds between successive task executions.
187 *java.util.Timer.schedule(TimerTask,long)*
189 public void schedule(
190 java.util.TimerTask task,
193 Schedules the specified task for execution after the specified delay.
195 task - task to be scheduled.
196 delay - delay in milliseconds before task is to be executed.
198 *java.util.Timer.schedule(TimerTask,long,long)*
200 public void schedule(
201 java.util.TimerTask task,
205 Schedules the specified task for repeated fixed-delay execution, beginning
206 after the specified delay. Subsequent executions take place at approximately
207 regular intervals separated by the specified period.
209 In fixed-delay execution, each execution is scheduled relative to the actual
210 execution time of the previous execution. If an execution is delayed for any
211 reason (such as garbage collection or other background activity), subsequent
212 executions will be delayed as well. In the long run, the frequency of execution
213 will generally be slightly lower than the reciprocal of the specified period
214 (assuming the system clock underlying Object.wait(long) is accurate).
216 Fixed-delay execution is appropriate for recurring activities that require
217 "smoothness." In other words, it is appropriate for activities where it is more
218 important to keep the frequency accurate in the short run than in the long run.
219 This includes most animation tasks, such as blinking a cursor at regular
220 intervals. It also includes tasks wherein regular activity is performed in
221 response to human input, such as automatically repeating a character as long as
224 task - task to be scheduled.
225 delay - delay in milliseconds before task is to be executed.
226 period - time in milliseconds between successive task executions.
228 *java.util.Timer.scheduleAtFixedRate(TimerTask,Date,long)*
230 public void scheduleAtFixedRate(
231 java.util.TimerTask task,
232 java.util.Date firstTime,
235 Schedules the specified task for repeated fixed-rate execution, beginning at
236 the specified time. Subsequent executions take place at approximately regular
237 intervals, separated by the specified period.
239 In fixed-rate execution, each execution is scheduled relative to the scheduled
240 execution time of the initial execution. If an execution is delayed for any
241 reason (such as garbage collection or other background activity), two or more
242 executions will occur in rapid succession to "catch up." In the long run, the
243 frequency of execution will be exactly the reciprocal of the specified period
244 (assuming the system clock underlying Object.wait(long) is accurate).
246 Fixed-rate execution is appropriate for recurring activities that are sensitive
247 to absolute time, such as ringing a chime every hour on the hour, or running
248 scheduled maintenance every day at a particular time. It is also appropriate
249 for recurring activities where the total time to perform a fixed number of
250 executions is important, such as a countdown timer that ticks once every second
251 for ten seconds. Finally, fixed-rate execution is appropriate for scheduling
252 multiple repeating timer tasks that must remain synchronized with respect to
255 task - task to be scheduled.
256 firstTime - First time at which task is to be executed.
257 period - time in milliseconds between successive task executions.
259 *java.util.Timer.scheduleAtFixedRate(TimerTask,long,long)*
261 public void scheduleAtFixedRate(
262 java.util.TimerTask task,
266 Schedules the specified task for repeated fixed-rate execution, beginning after
267 the specified delay. Subsequent executions take place at approximately regular
268 intervals, separated by the specified period.
270 In fixed-rate execution, each execution is scheduled relative to the scheduled
271 execution time of the initial execution. If an execution is delayed for any
272 reason (such as garbage collection or other background activity), two or more
273 executions will occur in rapid succession to "catch up." In the long run, the
274 frequency of execution will be exactly the reciprocal of the specified period
275 (assuming the system clock underlying Object.wait(long) is accurate).
277 Fixed-rate execution is appropriate for recurring activities that are sensitive
278 to absolute time, such as ringing a chime every hour on the hour, or running
279 scheduled maintenance every day at a particular time. It is also appropriate
280 for recurring activities where the total time to perform a fixed number of
281 executions is important, such as a countdown timer that ticks once every second
282 for ten seconds. Finally, fixed-rate execution is appropriate for scheduling
283 multiple repeating timer tasks that must remain synchronized with respect to
286 task - task to be scheduled.
287 delay - delay in milliseconds before task is to be executed.
288 period - time in milliseconds between successive task executions.