java.lang.Object | |
↳ | sun.misc.Timer |
A Timer object is used by algorithms that require timed events. For example, in an animation loop, a timer would help in determining when to change frames. A timer has an interval which determines when it "ticks"; that is, a timer delays for the specified interval and then it calls the owner's tick() method. Here's an example of creating a timer with a 5 sec interval:
class Main implements Timeable { public void tick(Timer timer) { System.out.println("tick"); } public static void main(String args[]) { (new Timer(this, 5000)).cont(); } }A timer can be stopped, continued, or reset at any time. A timer's state is not stopped while it's calling the owner's tick() method. A timer can be regular or irregular. If in regular mode, a timer ticks at the specified interval, regardless of how long the owner's tick() method takes. While the timer is running, no ticks are ever discarded. That means that if the owner's tick() method takes longer than the interval, the ticks that would have occurred are delivered immediately. In irregular mode, a timer starts delaying for exactly the specified interval only after the tick() method returns. Synchronization issues: do not hold the timer's monitor while calling any of the Timer operations below otherwise the Timer class will deadlock.
Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
owner | This is the owner of the timer. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a timer object that is owned by 'owner' and
with the interval 'interval' milliseconds.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Continue the timer.
| |||||||||||
Returns the timer's interval.
| |||||||||||
Returns the remaining time before the timer's next tick.
| |||||||||||
Returns the time at which the timer was last stopped.
| |||||||||||
Returns true if this timer is stopped.
| |||||||||||
Resets the timer's remaining time to the timer's interval.
| |||||||||||
Changes the timer's interval.
| |||||||||||
In regular mode, a timer ticks at the specified interval,
regardless of how long the owner's tick() method takes.
| |||||||||||
Sets the remaining time before the timer's next tick.
| |||||||||||
Stops the timer.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
This is the owner of the timer. Its tick method is called when the timer ticks.
Creates a timer object that is owned by 'owner' and with the interval 'interval' milliseconds. The new timer object is stopped and is regular. getRemainingTime() return 'interval' at this point. getStopTime() returns the time this object was created.
owner | owner of the timer object |
---|---|
interval | interval of the timer in milliseconds |
Continue the timer. The next tick will come at getRemainingTime() milliseconds later. If the timer is not stopped, this call will be a no-op. This method is MT-safe; i.e. it is synchronized but for implementation reasons, the synchronized modifier cannot be included in the method declaration.
Returns the timer's interval.
Returns the remaining time before the timer's next tick. The return value is valid only if timer is stopped.
Returns the time at which the timer was last stopped. The return value is valid only if the timer is stopped.
Returns true if this timer is stopped.
Resets the timer's remaining time to the timer's interval. If the timer's running state is not altered.
Changes the timer's interval. The new interval setting does not take effect until after the next tick. This method does not alter the remaining time or the running state of the timer.
interval | new interval of the timer in milliseconds |
---|
In regular mode, a timer ticks at the specified interval, regardless of how long the owner's tick() method takes. While the timer is running, no ticks are ever discarded. That means that if the owner's tick() method takes longer than the interval, the ticks that would have occurred are delivered immediately. In irregular mode, a timer starts delaying for exactly the specified interval only after the tick() method returns.
Sets the remaining time before the timer's next tick. This method does not alter the timer's running state. This method is MT-safe; i.e. it is synchronized but for implementation reasons, the synchronized modifier cannot be included in the method declaration.
time | new remaining time in milliseconds. |
---|
Stops the timer. The amount of time the timer has already delayed is saved so if the timer is continued, it will only delay for the amount of time remaining. Note that even after stopping a timer, one more tick may still occur. This method is MT-safe; i.e. it is synchronized but for implementation reasons, the synchronized modifier cannot be included in the method declaration.