jwars.synch
Class Timer
- public class Timer
- The Timer class is used to deliver updates at regular intervals. Individual updates are approximately equidistant unless the updates cannot be executed in the alotted time, in which case the Timer will attempt to execute them as fast as possible at the risk of starvation of other threads. The framerate is self-adjusting such that it will never diverge from some internal timer such as System.currentTimeMillis(). Care should be taken that sufficient time is alotted for updates to be executed. During the update operation it is possible to check whether the Timer is late, and thus reschedule nonessential updates. The methods of this class are unsynchronized. This means that this Timer should be manipulated only through the update method of the Dynamical it updates.
Timer
public Timer(int delayMillis,
jwars.synch.Dynamical dynamical)
- Constructs a Timer object which will invoke the update method of argument Dynamical every delayMillis milliseconds as specified by the default Watch, which wraps System.currentTimeMillis().
- Parameters:
delayMillis
- int
dynamical
- Dynamical
Timer
public Timer(jwars.synch.Watch watch,
int delayMillis,
jwars.synch.Dynamical dynamical)
- Constructs a Timer object using argument Watch, which will invoke the update method of argument Dynamical every delayMillis milliseconds as specified by the Watch.
- Parameters:
watch
- the Watch of this Timer
delayMillis
- delay in milliseconds between ticks
dynamical
- the target of this Timer
start
public void start()
- Starts this Timer. Calling this method twice on the same Timer object will result in an IllegalThreadStateException being thrown.
pause
public void pause()
- Pauses this Timer. No more updates will be performed until unpause() is invoked. After being unpaused, the Timer will update at the normal rate, i.e. will not try to catch up with the Watch. Subsequent calls without unpause() will do nothing. While paused, the Timer will call the reportStasis() method of the target Dynamical with the same frequency with which it would ordinarily call update().
suspend
public void suspend()
- Suspends this Timer. While the Timer is suspended, no more updates will be performed until unpause() is invoked. After being unpaused, the Timer will catch up with the reading of the Watch as quickly as possible, i.e. consider itself "late" if it has been suspended for long enough. This method can be used if the application must still be running at full speed, but cannot presently continue due to lacking input. While paused, the Timer will call the reportStasis() method of the target Dynamical with the same frequency with which it would ordinarily call update().
unpause
public void unpause()
- Unpauses this Timer if it has been suspended or paused. If both paused and suspended, the Timer will catch up with the current reading of the Watch, minus the time after the pause() method was invoked.
getTime
public int getTime()
- Returns the number of frames currently executed.
- Returns:
- current frame count
isLate
public boolean isLate()
- Returns true if and only if the current framecount is below what which should have been executed as determined by the Watch.
- Returns:
- whether the Timer is late
isRunning
public boolean isRunning()
- Returns true if this Timer is currently running, false otherwise. Otherwise means either that the Timer has not yet been started, is paused or suspended.
- Returns:
- whether the Timer is running
isPaused
public boolean isPaused()
- Returns true if and only if this Timer is currently paused.
- Returns:
- whether the Timer is paused
isSuspended
public boolean isSuspended()
- Returns true if and only if this Timer is currently suspended.
- Returns:
- whether the Timer is suspended.
delay
public void delay(int milliSeconds)
- Delay this Timer by specified number of milliseconds. This number may be negative, forcing the Timer to keep up.
- Parameters:
milliSeconds
- number of milliseconds to delay
timeLeftMillis
private long timeLeftMillis()
- Returns the number of milliseconds left before the Timer will be late
- Returns:
- number of milliseconds left within current frame