All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class timer.Timer

java.lang.Object
   |
   +----timer.Timer

public class Timer
extends Object
implements Runnable
The simulated-time clock. This is a large class, and the interface is divided into several sections: The threading interaction between clients and the timer is nontrivial. The following rules of thumb should be observed.


Variable Index

 o global
The global timer.
 o MS
The time unit milliseconds
 o SEC
The time unit seconds.
 o US
The time unit microseconds

Method Index

 o BlockSimTimeFor(Thread)
Force simulated time to stall until this thread unblocks it or unregisters.
 o CancelTimeout(TimeoutID)
Given an ID from a previous SetTimeout, remove it from the queue if it is still pending.
 o CurrentSimTime()
Return the current simulator time in microseconds
 o PrintStatus()
Print out the status of the timer, including event queue and registered threads.
 o RegisterThread(Thread)
Tell the simulator that this thread cares about simulated time.
 o run()
main event processing loop: if we can advance time, then advance to the next event and process it.
 o SetTimeout(TimerClient, long, TimeUnit, Object)
SetTimeout: set a timeout for the future
 o Shutdown()
Stops the timer, not really necessary.
 o SimTimeIsBlockedFor(Thread)
Check whether this thread is blocking sim time.
 o UnblockSimTimeFor(Thread)
Remove the blocking constraint on this thread.
 o UnregisterThread(Thread)
Tell the simulator that this thread no longer cares about simulated time.
 o WaitFor(TimerClient, long, TimeUnit)
Block the current thread on behalf of the client for a specified number of simulated time units.
 o WaitUntil(TimerClient, long, TimeUnit)
Block the current thread until a specified simulates time.

Variables

 o SEC
 public static TimeUnit SEC
The time unit seconds. Time is maintained internally in microseconds, but these units allow more convenient specification of time periods in the public functions.

 o MS
 public static TimeUnit MS
The time unit milliseconds

 o US
 public static TimeUnit US
The time unit microseconds

 o global
 public static Timer global
The global timer. Set up at program startup

Methods

 o Shutdown
 public void Shutdown()
Stops the timer, not really necessary.

 o CurrentSimTime
 public long CurrentSimTime()
Return the current simulator time in microseconds

 o SetTimeout
 public synchronized TimeoutID SetTimeout(TimerClient client,
                                          long time,
                                          TimeUnit units,
                                          Object info)
SetTimeout: set a timeout for the future

Parameters:
client - the client to wake up when the time expires
time - the number of time units from now that the timeout will expire
units - the units of time, one of
  • Timer.SEC: seconds
  • Timer.MS: milliseconds
  • Timer.US: microseconds
info - the information about the timeout to pass back to the client when the timeout expires
See Also:
Timeout
 o CancelTimeout
 public synchronized void CancelTimeout(TimeoutID id)
Given an ID from a previous SetTimeout, remove it from the queue if it is still pending.

 o WaitFor
 public void WaitFor(TimerClient client,
                     long time,
                     TimeUnit units) throws IllegalTimerStateException, IllegalMonitorStateException
Block the current thread on behalf of the client for a specified number of simulated time units. The current thread must be registered, and must be currently blocking time. The current thread must also hold the client's monitor, which will be released for the duration of the wait.

Parameters:
client - the client to wait on behalf of
time - time units to sleep
units - units for time
 o WaitUntil
 public void WaitUntil(TimerClient client,
                       long time,
                       TimeUnit units) throws IllegalTimerStateException, IllegalMonitorStateException
Block the current thread until a specified simulates time. The current thread must be registered, and must be currently blocking time. The current thread must also hold the client's monitor, which will be released for the duration of the wait.

Parameters:
client - the client to wait on behalf of
time - time units to sleep
units - units for time
 o RegisterThread
 public synchronized void RegisterThread(Thread t) throws IllegalTimerStateException
Tell the simulator that this thread cares about simulated time. This allows the thread to use the function BlockSimTimeFor, UnblockSimTimeFor and SimTimeIsBlockedFor, as well as the monitor functions in TimerClient.

Simulated time advances only when all registered threads are not blocking sim time. When first registered, a thread is blocking sim time,

Parameters:
t - the thread to register
 o UnregisterThread
 public synchronized void UnregisterThread(Thread t) throws IllegalTimerStateException
Tell the simulator that this thread no longer cares about simulated time.

Parameters:
t - the thread to register
 o BlockSimTimeFor
 public synchronized void BlockSimTimeFor(Thread t) throws IllegalTimerStateException
Force simulated time to stall until this thread unblocks it or unregisters. Threads should avoid sleeping while blocking sim time.

In general it will probably not be necessary for any client to call this function, but it is available just in case.

Parameters:
t - the thread to block on behalf of
 o UnblockSimTimeFor
 public synchronized void UnblockSimTimeFor(Thread t) throws IllegalTimerStateException
Remove the blocking constraint on this thread.

In general it will probably not be necessary for any client to call this function, but it is available just in case.

Parameters:
t - the thread to unblock on behalf of
 o SimTimeIsBlockedFor
 public synchronized boolean SimTimeIsBlockedFor(Thread t) throws IllegalTimerStateException
Check whether this thread is blocking sim time.

In general it will probably not be necessary for any client to call this function, but it is available just in case.

Parameters:
t - the thread to check
Returns:
true if the thread is blocking sim time.
 o run
 public synchronized void run()
main event processing loop: if we can advance time, then advance to the next event and process it.

Clients should not call this function.

 o PrintStatus
 public void PrintStatus()
Print out the status of the timer, including event queue and registered threads. Should be synchronized, but this is useful as a debugging tool when things deadlock, so it is left unsychronized.


All Packages  Class Hierarchy  This Package  Previous  Next  Index