Class Timer
java.lang.Object
|
+----Timer
- public class Timer
- extends Object
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.
-
owner
- This is the owner of the timer.
-
Timer(Timeable, long)
- Creates a timer object that is owned by 'owner' and
with the interval 'interval' milliseconds.
-
cont()
-
Continue the timer.
-
getInterval()
-
Returns the timer's interval.
-
getRemainingTime()
-
Returns the remaining time before the timer's next tick.
-
getStopTime()
-
Returns the time at which the timer was last stopped.
-
getTimerThread()
-
-
isStopped()
-
Returns true if this timer is stopped.
-
reset()
-
Resets the timer's remaining time to the timer's interval.
-
setInterval(long)
- Changes the timer's interval.
-
setRegular(boolean)
- In regular mode, a timer ticks at the specified interval,
regardless of how long the owner's tick() method takes.
-
setRemainingTime(long)
- Sets the remaining time before the timer's next tick.
-
stop()
-
Stops the timer.
owner
public Timeable owner
- This is the owner of the timer. Its tick method is
called when the timer ticks.
Timer
public Timer(Timeable owner,
long interval)
- 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.
- Parameters:
- owner - owner of the timer object
- interval - interval of the timer in milliseconds
isStopped
public synchronized boolean isStopped()
- Returns true if this timer is stopped.
stop
public void stop()
- 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.
cont
public void cont()
- 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.
reset
public void reset()
- Resets the timer's remaining time to the timer's interval.
If the timer's running state is not altered.
getStopTime
public synchronized long getStopTime()
- Returns the time at which the timer was last stopped. The
return value is valid only if the timer is stopped.
getInterval
public synchronized long getInterval()
- Returns the timer's interval.
setInterval
public synchronized void setInterval(long interval)
- 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.
- Parameters:
- interval - new interval of the timer in milliseconds
getRemainingTime
public synchronized long getRemainingTime()
- Returns the remaining time before the timer's next tick.
The return value is valid only if timer is stopped.
setRemainingTime
public void setRemainingTime(long time)
- 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.
- Parameters:
- time - new remaining time in milliseconds.
setRegular
public synchronized void setRegular(boolean regular)
- 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.
getTimerThread
protected Thread getTimerThread()