|
CSP for Java (JCSP) 1.1-rc4 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.jcsp.lang.Guard org.jcsp.lang.CSTimer
public class CSTimer
This is a Guard
for setting timeouts in an Alternative
.
Guard
for setting timeouts in an
Alternative
. It also provides the current system time
and can set straight (i.e. committed) timeouts. The timeouts
are in terms of absolute time values - not relative delays.
Note: for those familiar with the occam multiprocessing language, CSTimer gives the semantics of the TIMER type (including its use as a guard in an ALT construct).
Warning: a CSTimer records the timeout value for use by an
Alternative
. Therefore, different CSTimers
must be used by different processes - the same CSTimer
must not be shared.
Implementation note: all CSTimers currently use the same System.currentTimeMillis time.
Alternative
class (see the examples
A Fair Multiplexor with a Timeout
and A Simple Traffic Flow Regulator).
Here, we just show its use for setting committed timeouts. Regular generates a regular stream of output on its out channel. The rate of output is determined by its interval parameter. Recall that timeouts implemented by CSTimer are in terms of absolute time values. Notice that the sequence of output times maintains an arithmetic progression. Any delays in completing each cycle (e.g. caused by the process scheduler or the lateness of the process synchronising with us to accept our data) will be compensated for automatically - the output sequence always returns to its planned schedule whenever it can.
import org.jcsp.lang.*; public class Regular implements CSProcess { final private ChannelOutput out; final private Integer N; final private long interval; public Regular (final ChannelOutput out, final int n, final long interval) { this.out = out; this.N = new Integer (n); this.interval = interval; } public void run () { final CSTimer tim = new CSTimer (); long timeout = tim.read (); // read the (absolute) time once only while (true) { out.write (N); timeout += interval; // set the next (absolute) timeout tim.after (timeout); // wait until that (absolute) timeout } } }For convenience, a
sleep
method that blocks for a specified
time period (in milliseconds) is also provided. This has the same semantics as
java.lang.Thread.sleep
.
[Note: programming a regular sequence of events is a little easier using
after
(as in the above) rather than sleep
.]
Alternative
,
Guard
Constructor Summary | |
---|---|
CSTimer()
|
Method Summary | |
---|---|
void |
after(long msecs)
Puts the process to sleep until an absolute time is reached. |
long |
getAlarm()
Returns the alarm value that has been set by the previous call to setAlarm(long) . |
long |
read()
Returns the current system time in msecs. |
void |
set(long msecs)
Deprecated. Use setAlarm(long) - this name caused confusion with
the idea of setting the current time (a concept that is not supported). |
void |
setAlarm(long msecs)
Sets the absolute timeout value that will trigger an Alternative select operation (when this CSTimer is one of the guards with which that Alternative was constructed). |
void |
sleep(long msecs)
Puts the process to sleep for a specified time (milliseconds). |
Methods inherited from class org.jcsp.lang.Guard |
---|
schedule |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public CSTimer()
Method Detail |
---|
public void setAlarm(long msecs)
msecs
- the absolute timeout value.public long getAlarm()
setAlarm(long)
.
public void set(long msecs)
setAlarm(long)
- this name caused confusion with
the idea of setting the current time (a concept that is not supported).
msecs
- the absolute timeout value.public long read()
public void after(long msecs)
msecs
- the absolute time awaited. Note: if this time has already been reached, this returns straight away.public void sleep(long msecs)
msecs
- the length of the sleep period. Note: if this is negative, this returns straight away.
|
CSP for Java (JCSP) 1.1-rc4 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |