org.elkoserver.foundation.timer
Class Timer

java.lang.Object
  extended by org.elkoserver.foundation.timer.Timer

public class Timer
extends Object

The master control object for scheduling timed events using timeouts and clocks. One-time events (controlled by Timeout objects) may be scheduled by calling either of the after() methods. Recurring events (controlled by Clock objects) may be scheduled by calling either of the every() methods.

Event notification is guaranteed to be prompt but not immediate: the event handler will be invoked no sooner than scheduled and as soon thereafter as possible, but no guarantees are offered that somewhat more time will not have passed than was requested. In particular, while the scheduling API lets you specify times with millisecond precision, millisecond accuracy in practice should not be assumed.


Method Summary
 Timeout after(long millis, TimeoutNoticer target)
          Sets a timeout for the specified number of milliseconds.
 Timeout after(long millis, TimeoutNoticer target, boolean synchronous)
          Sets a timeout for the specified number of milliseconds.
 Clock every(long resolution, TickNoticer target)
          Creates a new clock.
 Clock every(long resolution, TickNoticer target, boolean synchronous)
          Creates a new clock.
static Timer theTimer()
          Return the single permitted Timer instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

after

public Timeout after(long millis,
                     TimeoutNoticer target,
                     boolean synchronous)
Sets a timeout for the specified number of milliseconds. After the timer expires, target's noticeTimeout() method is called.

Parameters:
millis - How long to wait until timing out.
target - Object to be informed when the time comes.
synchronous - Flag controlling synchronous notification of the timeout. If true, notify synchronously; if false, post notification on the message queue. Note that great care should be taken with synchronous notification as it introduces threading issues; do not use synchronous notification unless you understand these issues thoroughly (and, by the way, you don't).
Returns:
a timeout object that can be used to cancel or identify the timeout.
See Also:
TimeoutNoticer

after

public Timeout after(long millis,
                     TimeoutNoticer target)
Sets a timeout for the specified number of milliseconds. After the timer expires, target's noticeTimeout() method is called. Notification is always asynchronous: this method is equivalent to the after(long,TimeoutNoticer,boolean) method where the synchronous argument is set to false.

Parameters:
millis - How long to wait until timing out.
target - Object to be informed when the time comes.
Returns:
a timeout object that can be used to cancel or identify the timeout.
See Also:
TimeoutNoticer

every

public Clock every(long resolution,
                   TickNoticer target,
                   boolean synchronous)
Creates a new clock. The new clock begins life stopped with its tick count at zero (start the clock ticking by calling its start() method).

Parameters:
resolution - The clock tick interval.
target - Object to be sent tick notifications.
synchronous - Flag controlling synchronous notification of clock ticks. If true, notify synchronously; if false, post notification on the message queue. Note that great care should be taken with synchronous notification as it introduces threading issues; do not use synchronous notification unless you understand these issues thoroughly.
Returns:
a new clock object according to the given parameters.
See Also:
TickNoticer

every

public Clock every(long resolution,
                   TickNoticer target)
Creates a new clock. The new clock begins life stopped with its tick count at zero (start the clock ticking by calling its start() method). Clock ticks are always asynchronous: this method is equivalent to the every(long,TickNoticer,boolean) method where the synchronous argument is set to false.

Parameters:
resolution - The clock tick interval.
target - Object to be sent tick notifications.
Returns:
a new clock object according to the given parameters.
See Also:
TickNoticer

theTimer

public static Timer theTimer()
Return the single permitted Timer instance.