scorej
Class NonSimulationClock

java.lang.Object
  |
  +--scorej.OmarClock
        |
        +--scorej.NonSimulationClock
All Implemented Interfaces:
java.lang.Runnable

public class NonSimulationClock
extends OmarClock

Non Simulation Mode clock. This clock is meant to be used for simulations that want to run in real (wall-clock) time as opposed to having a clock process manage time. The clock thread, which was the central controlling thread for SimulationClocks, basically just does a Thread.yield() here. The main features of the non-simulation clock are:

S.sleep() does a Thread.sleep().

Signals are delivered by a signal processor thread asynchronously. Once delivered, they are removed. Thus, it is up to the programmer to ensure that any waitForSignals are active before the corresponsing sendSignal gets sent. This can be tricky in a multi-threaded environment such as this one. For example, consider the following code snippet:

 S.join(Invocation.callStatic("waitForSignal", "S", s1),
        Invocation.callStatic("sendSignal","S", s1))
 
It is undetermined which thread (waitForSignal, or sendSignal) will execute first. With the NonSimulation clock, if the sendSignal goes off before the waitForSignal has executed long enough to create and register the SignalWait object, the waitForSignal will miss the send of s1. SignalWaitQueues are a solution to this problem.

See Also:
SignalWaitQueue

Nested Class Summary
 
Nested classes inherited from class scorej.OmarClock
OmarClock.Process, OmarClock.StatusObj
 
Field Summary
 
Fields inherited from class scorej.OmarClock
_clockSync, _isSleepingSync, _pausedSync, _pauseTime, _realTime, _realTimeFactor, _resourceMap, _sigProc, _sleeping, _stateChangedSync, _status, _statusTable, _statusTableSync, _thread, _time, _zeroTime, DEAD, INITIALIZED, KILLED, PAUSED, RUNNING, SUSPENDED, TRANSITION, UNINITIALIZED, WAITING_FOR_FUTURE, WAITING_FOR_RESOURCE, WAITING_FOR_SIGNAL, WORKING
 
Constructor Summary
NonSimulationClock()
          No argument constuctor.
NonSimulationClock(double realTimeFactor)
           
 
Method Summary
 double getTime()
          Return the total time elapsed in seconds since the clock started.
 void killClock()
          Override of killClock.
 void run()
          Run method is basically a no-op once the clock is actually started.
 void sleep(java.lang.Thread t, double sleepTime)
          Thread sleeps for sleepTime real-time seconds.
protected  void startSignalProcessor()
          Signal processor is a separate thread that processes signals in parallel to the clock's execution.
 
Methods inherited from class scorej.OmarClock
addThread, cleanoutStatusTable, clockThreadSleep, getClockStateString, getLastSuspendedTime, getPreviousThreadState, getRealTimeFactor, getResourceMap, getStatus, getStatusTable, getThread, getThreadState, getThreadStateString, getThreadStateUnsafe, interruptClock, isRealTime, lockStatusTable, pauseClock, pauseClock, printStatusTable, removeThread, resetClock, resume, runClock, setAcquiredResource, setRealTime, setThreadState, setThreadStateUnsafe, setTime, setWaitingForResource, startClock, suspend, unlockStatusTable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NonSimulationClock

public NonSimulationClock(double realTimeFactor)
Parameters:
realTimeFactor - double = factor which sleeps are divided by. For example, realTimeFactor = 2 will cause all sleeps to be half as long.

NonSimulationClock

public NonSimulationClock()
No argument constuctor. Default value of realTimeFactor is 1.0

Method Detail

startSignalProcessor

protected void startSignalProcessor()
Signal processor is a separate thread that processes signals in parallel to the clock's execution. The clock does not control this thread at all.


killClock

public void killClock()
Override of killClock. Make sure to kill the signalProcessor thread too

Overrides:
killClock in class OmarClock

sleep

public void sleep(java.lang.Thread t,
                  double sleepTime)
Thread sleeps for sleepTime real-time seconds. Change the Process object in the hashtable from the WORKING state to a sleeping state where sleepTime is the time to wake up at. This method then blocks (by trying to acquire the Process semaphore) until the clock thread wakes it up at (currentTime + sleepTime)

Specified by:
sleep in class OmarClock
Parameters:
t - The thread that wants to sleep.
sleepTime - How long the thread wants to sleep for

run

public void run()
Run method is basically a no-op once the clock is actually started. You can't really "pause" the simulation once it is running, and the clock has no control. The clock's time field is not very accurate, since it only gets updated when sleeps execute. The correct time can be derived by calling getTime.

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class OmarClock

getTime

public double getTime()
Return the total time elapsed in seconds since the clock started. This is calculated using the following formula:

diff = (current system time in millis) - (system time in millis when the clock started) diff * (1000 / real time factor)

Overrides:
getTime in class OmarClock