All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class jcsp.lang.Parallel

java.lang.Object
   |
   +----jcsp.lang.Parallel

public class Parallel
extends Object
implements CSProcess

Description

The Parallel class enables the simultaneous execution of one or more CSProcess's which halts the execution of the current CSProcess until all of the CSProcess's finish execution.

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.

Example

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();
   }
 }
 

Author:
P.D.Austin
See Also:
CSProcess, ProcessNetwork

Constructor Index

 o Parallel()
Construct a new Parallel construct without any processes.
 o Parallel(boolean)
Construct a new Parallel construct without any processes.
 o Parallel(CSProcess[])
Construct a new Parallel construct with the CSProcess's specified.
 o Parallel(CSProcess[], boolean)
Construct a new Parallel construct with the CSProcess's specified.

Method Index

 o addProcess(CSProcess)
Adds a new CSProcess to the parallel construct.
 o addProcess(CSProcess[])
Adds a new CSProcess for each of the elements of the array to the parallel construct.
 o insertProcessAt(CSProcess, int)
inserts a new CSProcess to the parallel construct at the specifed index.
 o removeProcess(CSProcess)
Removes a CSProcess from the parallel construct.
 o run()

Executes each of the CSProcess's associated with this Object once in parallel.

Constructors

 o Parallel
 public Parallel()
Construct a new Parallel construct without any processes.

 o Parallel
 protected Parallel(boolean priority)
Construct a new Parallel construct without any processes. If the priority parameter has the value true, priorities higher in the process list will be given a higher priority.

Parameters:
priority - indicates that different priorities should be given to processes.
 o Parallel
 public Parallel(CSProcess processes[])
Construct a new Parallel construct with the CSProcess's specified.

Parameters:
processes - The processes to be executed in parallel
 o Parallel
 protected Parallel(CSProcess processes[],
                    boolean priority)
Construct a new Parallel construct with the CSProcess's specified. that If the priority parameter has the value true, priorities higher in the process list will be given a higher priority.

Parameters:
processes - The processes to be executed in parallel
priority - indicates that different priorities should be given to processes.

Methods

 o addProcess
 public synchronized void addProcess(CSProcess process)
Adds a new CSProcess to the parallel construct. The CSProcess will be executed the next time run() is invoked.

Parameters:
process - The CSProcess to be added
 o insertProcessAt
 public synchronized void insertProcessAt(CSProcess process,
                                          int index)
inserts a new CSProcess to the parallel construct at the specifed index. The CSProcess will be executed the next time run() is invoked.

NOTE: This only makes sense if the priority flag was set to true. Use addProcess() instead as it is more efficient.

Parameters:
process - The CSProcess to be inserted
index - The index to insert the processes at.
 o addProcess
 public synchronized void addProcess(CSProcess processes[])
Adds a new CSProcess for each of the elements of the array to the parallel construct. The CSProcess's will be executed the next time run() is invoked.

Parameters:
processes - The CSProcess's to be added
 o removeProcess
 public synchronized void removeProcess(CSProcess process)
Removes a CSProcess from the parallel construct. The CSProcess will not be executed the next time run() is invoked.

Parameters:
process - The CSProcess to be removed
 o run
 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