All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object | +----jcsp.lang.Parallel
NOTE: For those familiar with the OCCAM programming language the Parallel class is equivalent to the PAR construct.
The CSProcess's are executed when the run() method is invoked.
Higher priority can be given to the processes at the begining of the process list by using the constructors with the priority flag set to true.
NOTE: The priority flag only indicates the preference to have the processes to be executed with different priorities. If the priority of the calling process is set to the maximum priority allowed by the Java vertual machine all the processes will be executed at the same priority. Also be aware that nested Parallel's with the priority flag set may give processes which should have a lower priority a higher one.
CSProcess's can be added to the Parallel class either via the constructor or the addProcess methods. If a call to addProcess is called while the run() method is executing the CSProcess added will not be executed until the next time run() is invoked.
CSProcess's can be removed from the Parallel class via the removeProcess method. If a call to removeProcess is called while the run() method is executing the CSProcess removed will not be removed until the next time run() is invoked.
The following example demonstrates how to generate a new CSProcess which uses several other CSProcess's which run in Parallel and terminate. The example then shows how to execute several CSProcess's in Parallel and wrap them up in another CSProcess.
import jcsp.lang.*; import jcsp.util.*; import jcsp.util.buildingblocks.*; class Swap implements CSProcess { private ChannelInput in1; private ChannelInput in2; private ChannelOutput out1; private ChannelOutput out2; public Swap(ChannelInput in1, ChannelInput in2, ChannelOutput out1, ChannelOutput out2) { this.in1 = in1; this.in2 = in2; this.out1 = out1; this.out2 = out2; } public void run() { ProcessRead[] inProcs = { new ProcessRead(in1), new ProcessRead(in2) }; ProcessWrite[] outProcs = { new ProcessWrite(out1), new ProcessWrite(out2) }; Parallel inPar = new Parallel(inProcs); Parallel outPar = new Parallel(outProcs); while (true) { inPar.run(); outProcs[0].value = inProcs[1].value; outProcs[1].value = inProcs[0].value; outPar.run(); } } } public class ParallelExample { public static void main(String[] argv) { final Channel a = new One2OneChannel(); final Channel b = new One2OneChannel(); final Channel c = new One2OneChannel(); final Channel d = new One2OneChannel(); new Parallel(new CSProcess[] { new CSProcess() { public void run() { a.write(""); while (true) { Object o = c.read(); a.write(o + " \n"); } } }, new Nos(b), new Swap(a, b, c, d), new Printer(d) }).run(); } }
Executes each of the CSProcess's associated with this Object once in parallel.
public Parallel()
protected Parallel(boolean priority)
public Parallel(CSProcess processes[])
protected Parallel(CSProcess processes[], boolean priority)
public synchronized void addProcess(CSProcess process)
public synchronized void insertProcessAt(CSProcess process, int index)
NOTE: This only makes sense if the priority flag was set to true. Use addProcess() instead as it is more efficient.
public synchronized void addProcess(CSProcess processes[])
public synchronized void removeProcess(CSProcess process)
public synchronized void run()
Executes each of the CSProcess's associated with this Object once in parallel. This method will return only when and if all the CSProcess's terminate.
NOTE: Only numProcesses - 1 Threads are created to run the processes in, the last process is executed in the current Thread.
All Packages Class Hierarchy This Package Previous Next Index