All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----jcsp.lang.ints.AlternativeInt
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
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)
}
}
}
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;
}
}
}
}
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;
}
}
}
}
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
public AlternativeInt()
public synchronized int select(AltingChannelInputInt c[])
public synchronized int select(AltingChannelInputInt c[],
long msecs)
public synchronized int select(AltingChannelInputInt c[],
long msecs,
int nsecs)
public int select(AltingChannelInputInt c[],
boolean skip)
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.
public synchronized int select(AltingChannelInputInt c[],
boolean guard[])
NOTE: The c and guard arrays must be of the same size.
public synchronized int select(AltingChannelInputInt c[],
boolean guard[],
long msecs)
NOTE: The c and guard arrays must be of the same size.
public synchronized int select(AltingChannelInputInt c[],
boolean guard[],
long msecs,
int nsecs)
NOTE: The c and guard arrays must be of the same size.
public int select(AltingChannelInputInt c[],
boolean guard[],
boolean skip)
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.
All Packages Class Hierarchy This Package Previous Next Index