All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----jcsp.lang.Alternative
The select method performs the Channel selection function. The method returns the index of the Channel within the array which should be read from. The developer must then read from that Channel. 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 Channels being ready to be read from.
NOTE: For those familiar with the OCCAM programming language the Alternative class is equivalent to the PRI ALT construct. Example
import jcsp.lang.*;
public class AltExample1 implements CSProcess {
private AltingChannelInput in1;
private AltingChannelInput in2;
private ChannelOutput out;
public AltExample1(AltingChannelInput in1, AltingChannelInput in2, ChannelOutput out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
Alternative alt = new Alternative();
AltingChannelInput[] altChans = {
in1,
in2
};
while (true) {
int index = alt.select(altChans);
altChans[index].read();
out.write(new Integer(index))
}
}
}
import jcsp.lang.*;
public class AltExample2 implements CSProcess {
private AltingChannelInput in1;
private AltingChannelInput in2;
private ChannelOutput out;
public AltExample2(AltingChannelInput in1, AltingChannelInput in2, ChannelOutput out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
Alternative alt = new Alternative();
AltingChannelInput[] 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.*;
public class AltExample3 implements CSProcess {
private AltingChannelInput in1;
private AltingChannelInput in2;
private ChannelOutput out;
public AltExample3(AltingChannelInput in1, AltingChannelInput in2, ChannelOutput out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
Alternative alt = new Alternative();
AltingChannelInput[] 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.*;
public class AltExample4 implements CSProcess {
private AltingChannelInput in1;
private AltingChannelInput in2;
private ChannelOutput out;
public AltExample4(AltingChannelInput in1, AltingChannelInput in2, ChannelOutput out) {
this.in1 = in1;
this.in2 = in2;
this.out = out;
}
public void run() {
Alternative alt = new Alternative();
AltingChannelInput[] 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:
Integer i = (Integer)in1.read();
sum += i.intValue();
numRead++;
break;
case 1:
in2.read();
out.write(new Integer(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 Alternative()
public synchronized int select(AltingChannelInput c[])
public synchronized int select(AltingChannelInput c[],
long msecs)
public synchronized int select(AltingChannelInput c[],
long msecs,
int nsecs)
public int select(AltingChannelInput 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(AltingChannelInput c[],
boolean guard[])
NOTE: The c and guard arrays must be of the same size.
public synchronized int select(AltingChannelInput c[],
boolean guard[],
long msecs)
NOTE: The c and guard arrays must be of the same size.
public synchronized int select(AltingChannelInput c[],
boolean guard[],
long msecs,
int nsecs)
NOTE: The c and guard arrays must be of the same size.
public int select(AltingChannelInput 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