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:
- Clock interface: read the time
- public void CurrentSimTime ()
- Timeout interface: establish timeouts that will fire some
time in the future
- public TimeoutID SetTimeout ()
- public void CancelTimeout (TimeoutID)
- Wait interface: block the current thread until some
time in the future.
- public void WaitFor (TimerClient, long, TimeUnit)
- public void WaitUntil (TimerClient, long, TimeUnit)
The threading interaction between clients and the timer
is nontrivial. The following rules of thumb should be
observed.
- All client classes should extend the class
TimerClient. This identifies them as clients
and gives them access to monitor routines that work
correctly with the timer.
- Clients which only want to set and receive timeouts should
have little trouble. They will need to override the
Timeout function with one of their own, and should
generally avoid blocking or infinite processing inside
this function. If they do need to block, they should use
either the modified routines in TimerClient or
the WaitFor or WaitUntil functions in
the timer. They should avoid using the builtin Java
blocking routines, as this typically leads to deadlock.
- Clients which want to implement traditional multithreaded
applications need to register and unregister all their
threads with the timer. Threads should remain registered
throughout their entire lifetime, but must be
unregistered when they terminate. Otherwise, these
clients should observe the same rules as timeout clients.
-
global
- The global timer.
-
MS
- The time unit milliseconds
-
SEC
- The time unit seconds.
-
US
- The time unit microseconds
-
BlockSimTimeFor(Thread)
- Force simulated time to stall until this thread unblocks
it or unregisters.
-
CancelTimeout(TimeoutID)
- Given an ID from a previous SetTimeout,
remove it from the queue if it is still pending.
-
CurrentSimTime()
- Return the current simulator time in microseconds
-
PrintStatus()
- Print out the status of the timer, including event queue
and registered threads.
-
RegisterThread(Thread)
- Tell the simulator that this thread cares about simulated
time.
-
run()
- main event processing loop: if we can advance time, then
advance to the next event and process it.
-
SetTimeout(TimerClient, long, TimeUnit, Object)
- SetTimeout: set a timeout for the future
-
Shutdown()
- Stops the timer, not really necessary.
-
SimTimeIsBlockedFor(Thread)
- Check whether this thread is blocking sim time.
-
UnblockSimTimeFor(Thread)
- Remove the blocking constraint on this thread.
-
UnregisterThread(Thread)
- Tell the simulator that this thread no
longer cares about simulated time.
-
WaitFor(TimerClient, long, TimeUnit)
- Block the current thread on behalf of the client
for a specified number
of simulated time units.
-
WaitUntil(TimerClient, long, TimeUnit)
- Block the current thread until a specified simulates time.
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.
MS
public static TimeUnit MS
- The time unit milliseconds
US
public static TimeUnit US
- The time unit microseconds
global
public static Timer global
- The global timer. Set up at program startup
Shutdown
public void Shutdown()
- Stops the timer, not really necessary.
CurrentSimTime
public long CurrentSimTime()
- Return the current simulator time in microseconds
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
CancelTimeout
public synchronized void CancelTimeout(TimeoutID id)
- Given an ID from a previous SetTimeout,
remove it from the queue if it is still pending.
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
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
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
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
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
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
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.
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.
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