All Packages Class Hierarchy This Package Previous Next Index
Class jcsp.lang.ints.AlternativeInt
java.lang.Object
|
+----jcsp.lang.ints.AlternativeInt
- public class AlternativeInt
- extends Object
Description
The AlternativeInt class enables a CSProcess to wait for data to become
available from one or more ChannelInts.
The select method performs the ChannelInt selection function. The method
returns the index of the ChannelInt within the array which should be
read from. The developer must then read from that ChannelInt. There are
several versions of this method which perform the same basic
functionality but provide extra features such as timeouts and guards.
The method will return -1 if the method completes without any ChannelInts
being ready to be read from.
NOTE: For those familiar with the OCCAM programming language the
AlternativeInt class is equivalent to the PRI ALT construct.
Example
Simple Alt
import jcsp.lang.int.*;
public class AltExample1 implements CSProcess {
private AltingChannelInputInt in1;
private AltingChannelInputInt in2;
private ChannelOutputInt out;
public AltExample1(AltingChannelInputInt in1, AltingChannelInputInt in2, ChannelOutputInt out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
AlternativeInt alt = new AlternativeInt();
AltingChannelInputInt[] altChans = {
in1,
in2
};
while (true) {
int index = alt.select(altChans);
altChans[index].read();
out.write(index)
}
}
}
Alt with Timeout
import jcsp.lang.int.*;
public class AltExample2 implements CSProcess {
private AltingChannelInputInt in1;
private AltingChannelInputInt in2;
private ChannelOutputInt out;
public AltExample2(AltingChannelInputInt in1, AltingChannelInputInt in2, ChannelOutputInt out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
AlternativeInt alt = new AlternativeInt();
AltingChannelInputInt[] altChans = {
in1,
in2
};
while (true) {
switch (alt.select(altChans, 100)) {
case 0:
out.write(in1.read());
break;
case 1:
out.write(in2.read());
break;
default:
// do something like update the screen here
break;
}
}
}
}
Alt with Skip
import jcsp.lang.int.*;
public class AltExample3 implements CSProcess {
private AltingChannelInputInt in1;
private AltingChannelInputInt in2;
private ChannelOutputInt out;
public AltExample3(AltingChannelInputInt in1, AltingChannelInputInt in2, ChannelOutputInt out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
AlternativeInt alt = new AlternativeInt();
AltingChannelInputInt[] altChans = {
in1,
in2
};
boolean skip = false;
while (true) {
switch (alt.select(altChans, skip)) {
case 0:
skip = true;
out.write(in1.read());
break;
case 1:
skip = false;
out.write(in2.read());
break;
default:
// do something like update the screen here
break;
}
}
}
}
Alt with Guards
import jcsp.lang.int.*;
public class AltExample4 implements CSProcess {
private AltingChannelInputInt in1;
private AltingChannelInputInt in2;
private ChannelOutputInt out;
public AltExample4(AltingChannelInputInt in1, AltingChannelInputInt in2, ChannelOutputInt out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
AlternativeInt alt = new AlternativeInt();
AltingChannelInputInt[] altChans = {
in1,
in2
};
boolean[] altGuards = {
false,
false
};
boolean skip = false;
int numRead = 0;
int sum = 0;
while (true) {
altGuards[0] = (numRead < 10);
altGuards[1] = (numRead = 10);
switch (alt.select(altChans, altGuards)) {
case 0:
int i = in1.read();
sum += i;
numRead++;
break;
case 1:
in2.read();
out.write(sum);
sum = 0;
numRead = 0;
break;
}
}
}
}
NOTE: There are also methods to perform Alt with Guards and Timeout and
Alt with Guards and Skip
- Author:
- P.D.Austin
-
AlternativeInt()
-
-
select(AltingChannelInputInt[])
- Returns the index of one of the ChannelInts which has data that can be
read.
-
select(AltingChannelInputInt[], boolean)
- Returns the index of one of the ChannelInts which has data that can be
read.
-
select(AltingChannelInputInt[], boolean[])
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard.
-
select(AltingChannelInputInt[], boolean[], boolean)
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard.
-
select(AltingChannelInputInt[], boolean[], long)
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard.
-
select(AltingChannelInputInt[], boolean[], long, int)
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard.
-
select(AltingChannelInputInt[], long)
- Returns the index of one of the ChannelInts which has data that can be
read.
-
select(AltingChannelInputInt[], long, int)
- Returns the index of one of the ChannelInts which has data that can be
read.
AlternativeInt
public AlternativeInt()
select
public synchronized int select(AltingChannelInputInt c[])
- Returns the index of one of the ChannelInts which has data that can be
read. The method will block until one of the ChannelInts becomes ready.
The index of the ChannelInt in the array will be returned.
- Parameters:
- c - the ChannelInts to be selected from
select
public synchronized int select(AltingChannelInputInt c[],
long msecs)
- Returns the index of one of the ChannelInts which has data that can be
read. The method will block until either one of the ChannelInts becomes
ready or the timeout expires (negative or 0 timeouts will timeout immediately).
If one of the ChannelInts is ready the index of that ChannelInt in the array
will be returned otherwise -1 will be returned to indicate that no
ChannelInts are ready.
- Parameters:
- c - the ChannelInts to be selected from
- msecs - the time in milli-seconds to wait for a ChannelInt to become ready
select
public synchronized int select(AltingChannelInputInt c[],
long msecs,
int nsecs)
- Returns the index of one of the ChannelInts which has data that can be
read. The method will block until either one of the ChannelInts becomes
ready or the timeout expires (negative or 0 timeouts will timeout immediately).
If one of the ChannelInts is ready the index of that ChannelInt in the array
will be returned otherwise -1 will be returned to indicate that no
ChannelInts are ready.
- Parameters:
- c - the ChannelInts to be selected from
- msecs - the time in milli-seconds to wait for a ChannelInt to become ready
- nsecs - the extra time above msecs to wait in nano-seconds
select
public int select(AltingChannelInputInt c[],
boolean skip)
- Returns the index of one of the ChannelInts which has data that can be
read. If the skip flag is true the method will not block if no ChannelInts
are ready otherwise it will block until one of the ChannelInts is ready.
If one of the ChannelInts is ready the index of that ChannelInt in the array
will be returned otherwise -1 will be returned to indicate that no
ChannelInts are ready.
NOTE: This method enables the development of a polling programs which
can consume extra processor time. Try to use one of the other select
methods.
- Parameters:
- c - the ChannelInts to be selected from
- skip - A Boolean indicating whether the method should block until data is ready
select
public synchronized int select(AltingChannelInputInt c[],
boolean guard[])
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard. The method will
block until one of the ChannelInts becomes ready. The index of the ChannelInt
in the array will be returned.
NOTE: The c and guard arrays must be of the same size.
- Parameters:
- c - the ChannelInts to be selected from
- guard - the guards for the ChannelInts
select
public synchronized int select(AltingChannelInputInt c[],
boolean guard[],
long msecs)
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard. The method will
block until either one of the ChannelInts becomes ready or the timeout
expires (negative or 0 timeouts will timeout immediately). If one of the
ChannelInts is ready the index of that ChannelInt in the array
will be returned otherwise -1 will be returned to indicate that no
ChannelInts are ready.
NOTE: The c and guard arrays must be of the same size.
- Parameters:
- c - the ChannelInts to be selected from
- guard - the guards for the ChannelInts
- msecs - the time in milli-seconds to wait for a ChannelInt to become ready
select
public synchronized int select(AltingChannelInputInt c[],
boolean guard[],
long msecs,
int nsecs)
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard. The method will
block until either one of the ChannelInts becomes ready or the timeout
expires (negative or 0 timeouts will timeout immediately). If one of the
ChannelInts is ready the index of that ChannelInt in the array
will be returned otherwise -1 will be returned to indicate that no
ChannelInts are ready.
NOTE: The c and guard arrays must be of the same size.
- Parameters:
- c - the ChannelInts to be selected from
- guard - the guards for the ChannelInts
- msecs - the time in milli-seconds to wait for a ChannelInt to become ready
- nsecs - the extra time above msecs to wait in nano-seconds
select
public int select(AltingChannelInputInt c[],
boolean guard[],
boolean skip)
- Returns the index of one of the ChannelInts which has data that can be
read ignoring those ChannelInts which have a false guard.
If the skip flag is true the method will not block if no ChannelInts
are ready otherwise it will block until one of the ChannelInts is ready.
If one of the ChannelInts is ready the index of that ChannelInt in the array
will be returned otherwise -1 will be returned to indicate that no
ChannelInts are ready.
NOTE: The c and guard arrays must be of the same size.
NOTE: This method enables the development of a polling programs which
can consume extra processor time. Try to use one of the other select
methods.
- Parameters:
- c - the ChannelInts to be selected from
- guard - the guards for the ChannelInts
- skip - A Boolean indicating whether the method should block until data is ready
All Packages Class Hierarchy This Package Previous Next Index