public class Countdown extends Object implements Runnable
A Countdown can be used in two ways but you should not use the same Countdown object in both ways. It can be used as a simple lookup where a calling thread calls start() to mark the current time and then can call hasExpired(), timeRemaining() and elapse() to check the state of the Countdown. No extra Thread is created using these methods.
Or it can be used to automatically generate a "tick" after a specified interval via a separate Thread that it creates for this purpose. To do that you call one of the requestTick() methods. Note that unlike a Timer, a Countdown can only service one "tick" at a time. Any call to requestTick() will override previous calls.
Modifier and Type | Field and Description |
---|---|
long |
idleQuitTime
This is used with the requestTick() methods.
|
protected long |
interval |
protected Thread |
myTicker |
protected long |
started |
protected TimerProc |
tickProc |
Constructor and Description |
---|
Countdown()
Create a new Countdown that will not expire.
|
Modifier and Type | Method and Description |
---|---|
void |
cancelTick()
Cancel any pending tick request and set the interval to -1.
|
void |
cancelTick(TimerProc forWho) |
long |
elapsed()
Return the amount of time that has elapsed since started was called or
since requestTick() was called.
|
protected void |
finalize()
This method (when overriden), will be called when the VM determines that the Object can be
garbage collected.
|
boolean |
hasExpired()
Returns if the time has expired.
|
void |
requestTick(long howLong)
Request that the Countdown tick() method be called after the specified interval.
|
void |
requestTick(long howLong,
boolean cancelPrevious)
Request that the Countdown tick() method be called after the specified interval.
|
void |
requestTick(TimerProc proc,
long howLong)
Request a tick to be called on a TimerProc after the specified interval.
|
void |
run()
Do not call this method directly.
|
Countdown |
start(long howLongInMillis)
Start the count down for the specified time.
|
Countdown |
start(TimeOut remaining)
Start counting down from the remaining time left on the TimeOut.
|
protected void |
ticked()
Override this to handle ticks.
|
long |
timeRemaining()
Returns the time remaining before the interval expires.
|
boolean |
waitOn(Object obj)
Wait on the Object for the length of time remaining.
|
void |
waitUntilExpired() |
protected long interval
protected long started
protected Thread myTicker
protected TimerProc tickProc
public long idleQuitTime
public Countdown()
public Countdown start(TimeOut remaining)
remaining
- a TimeOut object. The remaining() method is called on
this if it is not null and start() is called using that value. If remaining
is null then an infinite interval is used.public Countdown start(long howLongInMillis)
howLongInMillis
- the interval in milliseconds.public boolean hasExpired()
public long timeRemaining()
public boolean waitOn(Object obj) throws InterruptedException
obj
- the object to wait on.InterruptedException
- if the wait was interrupted.public void waitUntilExpired() throws IllegalStateException
IllegalStateException
public long elapsed()
public void requestTick(long howLong)
howLong
- the length of time to wait before calling the tick() method.public void requestTick(long howLong, boolean cancelPrevious)
howLong
- the length of time to wait before calling the tick() method.cancelPrevious
- If the Countdown is already processing a previous
tick request and this is true, then the previous tick request is cancelled
and the countdown begins again for howLong. Otherwise if cancelPrevious is
false, and ther is a pending tick, then this new request is ignored.public void requestTick(TimerProc proc, long howLong)
proc
- an optional TimerProc object to be called.howLong
- how long in milliseconds to wait before calling
the ticked() method.public void cancelTick()
public void cancelTick(TimerProc forWho)
protected void ticked()
protected void finalize()
Object
The finalize() method of a Class is only called if it overrides finalize() - the finalize() method java.lang.Object is never called by the Eve VM.