Java.util
Class Timer
Java.lang.Object
Java.util.Timer
Timerextends Object
A tool that the thread uses to schedule tasks to perform later in a background thread. You can schedule a task to execute once, or repeat it periodically.
corresponding to each timer object is a single background thread, which is used to perform all timer tasks sequentially. The timer task should be completed quickly. If the time to complete a timer task is too long, it will "monopolize" the task execution thread of the timer. As a result, this can delay the execution of subsequent tasks, which can be "stacked together" and can be executed quickly and continuously when the unfriendly task is finally completed.
After the last reference to the Timer object is complete, and after all the unhandled tasks have been completed, the task execution thread of the timer terminates normally (and becomes the garbage-collected object). But it may take a long time before it happens. By default, the task execution thread does not run as a daemon thread , so it can prevent the application from terminating. If the caller wants to quickly terminate the task execution thread of the timer, the caller should invoke the timer's cancel method.
If a timer's task execution thread is terminated unexpectedly, such as a stop method that calls it, all subsequent attempts to schedule a task on that timer will cause illegalstateexception, as if a timer was called Cancel method.
This class is thread-safe: Multiple threads can share a single Timer object without having to synchronize externally.
This class does not provide a real-time guarantee: It uses the object.wait (Long) method to schedule tasks.
Implementation considerations: This class can be extended to a large number of simultaneous scheduled tasks (there are thousands of of them without problems). Internally, it uses a binary heap to represent its task queue, so the overhead of scheduling a task is O (log n), where n is the number of simultaneous tasks scheduled.
Implementation considerations: All construction methods start the timer thread.
-
Start from the following versions:
-
1.3
-
See also:
-
TimerTask
,
Object.wait(long)
Construction Method Summary |
Timer() Creates a new timer. |
Timer(boolean isDaemon) Creates a new timer that specifies its associated thread to run as a daemon. |
Timer(String name) Creates a new timer whose associated thread has the specified name. |
Timer(String name, boolean isDaemon) Creates a new timer whose associated thread has the specified name and can be specified to run as a daemon. |
Method Summary |
void |
cancel() Terminates this timer, discarding all currently scheduled tasks. |
int |
purge() Removes all canceled tasks from this timer's task queue. |
void |
schedule(TimerTask task, Date time) Schedules the specified task to execute at the specified time. |
void |
schedule(TimerTask task, Date firstTime, long period) Schedules the specified task to begin repeating fixed deferred execution at a specified time. |
void |
schedule(TimerTask task, long delay) Schedules the specified task to execute after a specified delay. |
void |
schedule(TimerTask task, long delay, long period) Schedules the specified task to start repeating fixed deferred execution from the specified delay. |
void |
scheduleAtFixedRate(TimerTask task, Date firstTime, long period) Schedules the specified task to start at a fixed rate of repetition at a specified time. |
void |
scheduleAtFixedRate(TimerTask task, long delay, long period) Schedules the specified task to begin repeating a fixed rate of execution after a specified delay. |
Methods of inheriting from class Java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Construction Method Details |
Timer
Timer ()
-
creates a new timer. The associated thread
is not running as a daemon.
-
See also:
-
Thread
,
cancel()
Timer
Timer (Boolean Isdaemon)
-
Creates a new timer that specifies its associated thread to run as a daemon. If the timer will be used to schedule duplicate "maintenance activities," the daemon thread must be called, but the operation should not prolong the lifetime of the program if the daemon is invoked during the application's run.
-
Parameters:
-
isDaemon
-True if the related thread should be run as a daemon.
-
See also:
-
Thread
,
cancel()
Timer
Timer (String name)
-
Creates a new timer whose associated thread has the specified name. The associated thread
is not running as a daemon.
-
Parameters:
-
name
-The name of the related thread.
-
Thrown:
-
NullPointerException
-if name is null.
-
Start from the following versions:
-
1.5
-
See also:
-
Thread.getName()
,
Thread.isDaemon()
Timer
Timer (String name, boolean Isdaemon)
-
Creates a new timer whose associated thread has the specified name and can be specified to run as a daemon.
-
Parameters:
-
name
-The name of the related thread.
-
isDaemon
-True if the related thread should be run as a daemon.
-
Thrown:
-
NullPointerException
-if name is null.
-
Start from the following versions:
-
1.5
-
See also:
-
Thread.getName()
,
Thread.isDaemon()
Schedule
Schedule (timertask task, long delay)
-
Schedules The specified task to execute after a specified delay.
-
-
Parameters:
-
task
-The tasks to be scheduled.
-
delay
-The delay time before the task is performed, in milliseconds.
-
Thrown:
-
IllegalArgumentException
-If delay is a negative number, or delay + system.currenttimemillis () is a negative number.
-
IllegalStateException
-If the task has been scheduled or canceled, or the timer has been canceled.
Schedule
Schedule (TimerTask task, Date time)
-
Schedules the specified task to execute at the specified time. If this time has passed, schedule the task to execute immediately.
-
-
Parameters:
-
task
-The tasks to be scheduled.
-
time
-time to perform the task.
-
Thrown:
-
IllegalArgumentException
-If time.gettime () is a negative number.
-
IllegalStateException
-If the task has been scheduled or canceled, the timer has been canceled, or the timer thread has terminated.
Schedule
Schedule (timertask task, long delay, long period)
-
Schedules the specified task to start repeating
fixed deferred execution from the specified delay. Follow-up is performed at approximately fixed intervals (delimited by the specified period).
In a fixed deferred execution, each execution is scheduled based on the actual execution time of the previous execution. If an execution is delayed for any reason, such as a garbage collection or other background activity, subsequent executions will also be deferred. In the long run, the frequency of execution is generally slightly slower than the reciprocal of the specified period (assuming that the system clock relied on by object.wait (long) is accurate).
Fixed-delay execution applies to repetitive activities that require "smooth" running. In other words, it is suitable for activities that are more accurate in the short run than those that are more important in the long run. This includes most animation tasks, such as the blinking cursor at fixed intervals. This also includes fixed activities that are performed in response to human activity, such as automatically repeating the input characters while holding down the key.
-
-
Parameters:
-
task
-The tasks to be scheduled.
-
delay
-The delay time before the task is performed, in milliseconds.
-
period
-the time interval between each successor task, in milliseconds.
-
Thrown:
-
IllegalArgumentException
-If delay is a negative number, or delay + system.currenttimemillis () is a negative number.
-
IllegalStateException
-If the task has been scheduled or canceled, the timer has been canceled, or the timer thread has terminated.
Schedule
public void Schedule (timertask task, Date firsttime, long PE RIOD)
-
Schedules the specified task to begin repeating
fixed deferred execution at a specified time. Follow-up is performed at approximately fixed intervals (delimited by the specified period).
In a fixed deferred execution, each execution is scheduled based on the actual execution time of the previous execution. If an execution is delayed for any reason, such as a garbage collection or other background activity, subsequent executions will also be deferred. In the long run, the frequency of execution is generally slightly slower than the reciprocal of the specified period (assuming that the system clock relied on by object.wait (long) is accurate).
Fixed deferred execution is for repetitive execution activities that require "smooth" running. In other words, it is suitable for activities that are more accurate in the short run than those that are more important in the long run. This includes most animation tasks, such as the blinking cursor at fixed intervals. This also includes fixed activities that are performed in response to human activity, such as automatically repeating the input characters while holding down the key.
-
-
Parameters:
-
task
-The tasks to be scheduled.
-
firstTime
-time when the task was first executed.
-
period
-the time interval between each successor task, in milliseconds.
-
Thrown:
-
IllegalArgumentException
-If time.gettime () is a negative number.
-
IllegalStateException
-If the task has been scheduled or canceled, the timer has been canceled, or the timer thread has terminated.
Scheduleatfixedrate
public void scheduleatfixedrate (timertask task, long delay, Long period)
-
Schedules the specified task to begin repeating a
fixed rate of execution after a specified delay. Follow-up is performed at approximately fixed intervals (delimited by the specified period).
In a fixed rate execution, each execution is scheduled according to the scheduled initial execution time. If an execution is delayed for any reason, such as a garbage collection or other background activity, two or more executions will occur quickly and continuously, allowing subsequent executions to "catch up". In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming that the system clock relied on by object.wait (long) is accurate).
Fixed rate execution applies to repetitive execution activities that are sensitive to absolute time, such as hourly timekeeping, or running scheduled maintenance activities at specific times of the day. It also applies to repetitive activities that are important for the total time to complete a fixed number of executions, such as a countdown timer that ticks once every second for 10 seconds. Finally, fixed rate execution is appropriate for scheduling multiple recurring timer tasks that must be synchronized with each other.
-
-
Parameters:
-
task
-The tasks to be scheduled.
-
delay
-The delay time before the task is performed, in milliseconds.
-
period
-the time interval between each successor task, in milliseconds.
-
Thrown:
-
IllegalArgumentException
-If delay is a negative number, or delay + system.currenttimemillis () is a negative number.
-
IllegalStateException
-If the task has been scheduled or canceled, the timer has been canceled, or the timer thread has terminated.
Scheduleatfixedrate
scheduleatfixedrate (TimerTask task, Date Firsttime, long period)
-
Schedules the specified task to start at a
fixed rate of repetition at a specified time. Follow-up is performed at approximately fixed intervals (delimited by the specified period).
In a fixed rate execution, each execution is scheduled relative to the scheduled initial execution time. If an execution is delayed for any reason, such as a garbage collection or other background activity, the execution will occur two or more times in a rapid succession, so that subsequent executions can catch up. In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming that the system clock relied on by object.wait (long) is accurate).
Fixed rate execution applies to repetitive execution activities that are sensitive to absolute time, such as hourly timekeeping, or running scheduled maintenance activities at specific times of the day. It also applies to repetitive activities that are important for the total time to complete a fixed number of executions, such as a countdown timer that ticks once every second for 10 seconds. Finally, the fixed rate execution applies to timer tasks that schedule repeated executions, which must be synchronized with each other.
-
-
Parameters:
-
task
-The tasks to be scheduled.
-
firstTime
-time when the task was first executed.
-
period
-the time interval between each successor task, in milliseconds.
-
Thrown:
-
IllegalArgumentException
-If time.gettime () is a negative number.
-
IllegalStateException
-If the task has been scheduled or canceled, the timer has been canceled, or the timer thread has terminated.
Cancel
Cancel ()
-
Terminates this timer, discarding all currently scheduled tasks. This does not interfere with the currently executing task, if it exists. Once the timer is terminated, its execution thread terminates, and no more tasks can be scheduled on it.
Note that this method is called within the Run method of the timer task called by this timer, and you can absolutely ensure that the task being performed is the last task that this timer performs.
This method can be called repeatedly, but the second and subsequent calls are not valid.
-
Purge
Purge ()
-
removes all canceled tasks from this timer's task queue.
calling this method has no effect on the behavior of the timer, but it will not reference the canceled task in the queue. If there are no external references to these tasks, they become eligible objects for garbage collection.
Most programs do not need to call this method. It is designed for some rare applications that can cancel a large number of tasks. Call this method to exchange space for time: This method may run with n + C log n proportional, where n is the number of tasks in the queue, and C is the number of canceled tasks.
Note that calling this method is allowed on tasks scheduled from this timer.
-
-
Return:
-
The number of tasks to remove from the queue.
-
Start from the following versions:
-
1.5
Java Timer class