All Packages Class Hierarchy This Package Previous Next Index
Class jcsp.lang.Alternative
java.lang.Object
|
+----jcsp.lang.Alternative
- public class Alternative
- extends Object
Description
The Alternative class enables a CSProcess to wait for data to become
available from one or more Channels.
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
Simple Alt
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))
}
}
}
Alt with Timeout
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;
}
}
}
}
Alt with Skip
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;
}
}
}
}
Alt with Guards
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
- Author:
- P.D.Austin
-
Alternative()
-
-
select(AltingChannelInput[])
- Returns the index of one of the Channels which has data that can be
read.
-
select(AltingChannelInput[], boolean)
- Returns the index of one of the Channels which has data that can be
read.
-
select(AltingChannelInput[], boolean[])
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard.
-
select(AltingChannelInput[], boolean[], boolean)
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard.
-
select(AltingChannelInput[], boolean[], long)
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard.
-
select(AltingChannelInput[], boolean[], long, int)
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard.
-
select(AltingChannelInput[], long)
- Returns the index of one of the Channels which has data that can be
read.
-
select(AltingChannelInput[], long, int)
- Returns the index of one of the Channels which has data that can be
read.
Alternative
public Alternative()
select
public synchronized int select(AltingChannelInput c[])
- Returns the index of one of the Channels which has data that can be
read. The method will block until one of the Channels becomes ready.
The index of the Channel in the array will be returned.
- Parameters:
- c - the Channels to be selected from
select
public synchronized int select(AltingChannelInput c[],
long msecs)
- Returns the index of one of the Channels which has data that can be
read. The method will block until either one of the Channels becomes
ready or the timeout expires (negative or 0 timeouts will timeout immediately).
If one of the Channels is ready the index of that Channel in the array
will be returned otherwise -1 will be returned to indicate that no
Channels are ready.
- Parameters:
- c - the Channels to be selected from
- msecs - the time in milli-seconds to wait for a Channel to become ready
select
public synchronized int select(AltingChannelInput c[],
long msecs,
int nsecs)
- Returns the index of one of the Channels which has data that can be
read. The method will block until either one of the Channels becomes
ready or the timeout expires (negative or 0 timeouts will timeout immediately).
If one of the Channels is ready the index of that Channel in the array
will be returned otherwise -1 will be returned to indicate that no
Channels are ready.
- Parameters:
- c - the Channels to be selected from
- msecs - the time in milli-seconds to wait for a Channel to become ready
- nsecs - the extra time above msecs to wait in nano-seconds
select
public int select(AltingChannelInput c[],
boolean skip)
- Returns the index of one of the Channels which has data that can be
read. If the skip flag is true the method will not block if no Channels
are ready otherwise it will block until one of the Channels is ready.
If one of the Channels is ready the index of that Channel in the array
will be returned otherwise -1 will be returned to indicate that no
Channels 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 Channels to be selected from
- skip - A Boolean indicating whether the method should block until data is ready
select
public synchronized int select(AltingChannelInput c[],
boolean guard[])
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard. The method will
block until one of the Channels becomes ready. The index of the Channel
in the array will be returned.
NOTE: The c and guard arrays must be of the same size.
- Parameters:
- c - the Channels to be selected from
- guard - the guards for the Channels
select
public synchronized int select(AltingChannelInput c[],
boolean guard[],
long msecs)
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard. The method will
block until either one of the Channels becomes ready or the timeout
expires (negative or 0 timeouts will timeout immediately). If one of the
Channels is ready the index of that Channel in the array
will be returned otherwise -1 will be returned to indicate that no
Channels are ready.
NOTE: The c and guard arrays must be of the same size.
- Parameters:
- c - the Channels to be selected from
- guard - the guards for the Channels
- msecs - the time in milli-seconds to wait for a Channel to become ready
select
public synchronized int select(AltingChannelInput c[],
boolean guard[],
long msecs,
int nsecs)
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard. The method will
block until either one of the Channels becomes ready or the timeout
expires (negative or 0 timeouts will timeout immediately). If one of the
Channels is ready the index of that Channel in the array
will be returned otherwise -1 will be returned to indicate that no
Channels are ready.
NOTE: The c and guard arrays must be of the same size.
- Parameters:
- c - the Channels to be selected from
- guard - the guards for the Channels
- msecs - the time in milli-seconds to wait for a Channel to become ready
- nsecs - the extra time above msecs to wait in nano-seconds
select
public int select(AltingChannelInput c[],
boolean guard[],
boolean skip)
- Returns the index of one of the Channels which has data that can be
read ignoring those Channels which have a false guard.
If the skip flag is true the method will not block if no Channels
are ready otherwise it will block until one of the Channels is ready.
If one of the Channels is ready the index of that Channel in the array
will be returned otherwise -1 will be returned to indicate that no
Channels 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 Channels to be selected from
- guard - the guards for the Channels
- skip - A Boolean indicating whether the method should block until data is ready
All Packages Class Hierarchy This Package Previous Next Index