public class Parallel
This process constructor taks an array of CSProcesses and returns a CSProcess that is the parallel composition of its process arguments.
Shortcut to the Constructor and Method Summaries.
Note: for those familiar with the occam multiprocessing language, the Parallel class gives the semantics of the PAR construct. However, none of the parallel usage checks mandated by occam can be made by the Java compiler, so we need to exercise that care ourselves. For instance, do not try to run the same process instance more than once in parallel and, generally, watch out for accidentally shared objects! Running different instances of the same process in parallel is, of course, allowed.
CSProcesses can be added to a Parallel object either via the constructor or the addProcess<TT>addProcess</TT> methods. If a call to addProcess is made while the run method is executing, the extra process(es) will not be included in the network until the next time run is invoked.
CSProcesses can be removed from a Parallel object via the removeProcess<TT>removeProcess</TT> or removeAllProcesses<TT>removeAllProcesses</TT> method. If a call to removeProcess or removeAllProcesses is made while the run method is executing, the process will not be removed from the network until the next time run is invoked.
Note: to add/remove a process to/from a network whilst it is running, see the ProcessManager class.
import org.jcsp.lang.*; import org.jcsp.plugNplay.*; class ParaplexIntTest { public static void main (String[] args) { final One2OneChannelInt[] a = ChannelInt.createOne2One (3); final One2OneChannel b = Channel.createOne2One (); new Parallel ( new CSProcess[] { new NumbersInt (a[0].out ()), new SquaresInt (a[1].out ()), new FibonacciInt (a[2].out ()), new ParaplexInt (ChannelInt.getInputArray (a), b.out ()), new CSProcess () { public void run () { System.out.println ("\n\t\tNumbers\t\tSquares\t\tFibonacci\n"); while (true) { int[] data = (int[]) b.in ().read (); for (int i = 0; i < data.length; i++) { System.out.print ("\t\t" + data[i]); } System.out.println (); } } } } ).run (); } }This example tabulates columns of (respectively) natural numbers, perfect squares and the Fibonacci sequence. At this level, we are only aware of five communicating processes: three that generate the respective sequences of integers, one that multiplexes a single item from each sequence into a single packet and the in-lined process that receives this packet and tabulates its contents. And, at this level, that is all we need to think about.
However, clicking on any of the generator processes reveals sub-networks (and, in the case of SquaresInt and FibonacciInt, sub-sub-networks). Altogether, the example contains 28 parallel processes -- 18 of them high-level (and non-terminating) and 10 low-level (and transient, but repeatedly re-invoked). One of the key benefits of CSP is that its semantics are compositional -- i.e. we do not have to reason about all those 28 processes at the same time to reason about how they behave in this application. We can build up the complexity in layers.
Note: the above example is just to build fluency with the CSP/occam concept of parallel composition and to show how easy it is. The network decomposes into fine-grained stateless components that would be excellent if we were refining this application down to a silicon (e.g. FPGA) implementation -- but for software running on a uni-processor JVM, we would not suggest going quite so far!
Note: the above layered network of communicating parallel processes is completely deterministic. It will produce the same results regardless of the scheduling characteristics of the underlying JVM and regardless of its physical distribution on to separate processors (and their relative speeds). This default determinism is one of the founding strengths of CSP concurrency that reinforces confidence in the systems we build with it.
Non-determinism, of course, needs to be addressed for many applications and is catered for in JCSP by its Alternative construct (which corresponds to the CSP external choice operator and occam ALT), by its any-1, 1-any and any-any channels (e.g. Any2OneChannel) and by the overwriting semantics that can be defined for its channels (e.g. OverWriteOldestBuffer). The fact that non-determinism has to be explicitly introduced reduces the chance of overlooking race-hazards caused by that non-determinism.
package org.jcsp.plugNplay.ints; import org.jcsp.lang.*; public final class ParaplexInt implements CSProcess { private final ChannelInputInt[] in; private final ChannelOutput out; public ParaplexInt (final ChannelInputInt[] in, final ChannelOutput out) { this.in = in; this.out = out; } public void run () { final ProcessReadInt[] inputProcess = new ProcessReadInt[in.length]; for (int i = 0; i < in.length; i++) { inputProcess[i] = new ProcessReadInt (in[i]); } Parallel parInput = new Parallel (inputProcess); int[][] data = new int[2][in.length]; // double-buffer int index = 0; // initial buffer index while (true) { parInput.run (); int[] buffer = data[index]; // grab a buffer for (int i = 0; i < in.length; i++) { buffer[i] = inputProcess[i].value; } out.write (buffer); index = 1 - index; // switch buffers } } }Note that the Parallel object (parInput) is constructed once and contains an array of processes (ProcessReadInt), each of which performs only a single channel input and, then, terminates. Each time it is run (parInput.run inside the loop), all those sub-processes run concurrently -- the parallel run terminating when, and only when, all those sub-processes have terminated. See the documentation of ParaplexInt for the motivation for this low-level concurrency (and for the double-buffering).
If a Parallel process has finished its run() and is not going to be used again, its parked threads may be unparked and terminated by invoking its releaseAllThreads<TT>releaseAllThreads</TT> method. This will release the memory used by those threads.
Type | Name and description |
---|---|
void |
addProcess(CSProcess process) Add the process to the Parallel object. |
void |
addProcess(CSProcess[] newProcesses) Add the array of processes to the Parallel object. |
static void |
destroy() Stops all threads created by all Parallel and ProcessManager objects. |
protected void |
finalize() System finalizer. |
int |
getNumberProcesses() @return the number of processes currently registered. |
void |
releaseAllThreads() Release all threads saved by the Parallel object for future runs - the threads all terminate and release their associated workspaces. |
void |
removeAllProcesses() Remove all processes from the Parallel object. |
void |
removeProcess(CSProcess process) Remove the process from the Parallel object. |
static void |
resetDestroy() Cancels a call to destroy allowing the JCSP system to be reused. |
void |
run() Run the parallel composition of the processes registered with this Parallel object. |
static void |
setUncaughtErrorDisplay(boolean enable) Enables or disables the display or Errors uncaught by a CSProcess running within a Parallel or under a ProcessManager object. |
static void |
setUncaughtExceptionDisplay(boolean enable) Enables or disables the display of Exceptions uncaught by a CSProcess running within a Parallel or under a ProcessManager object. |
Construct a new Parallel object initially without any processes.
Construct a new Parallel object with the processes specified.
processes
- The processes to be executed in parallelAdd the process to the Parallel object. The extended network will be executed the next time run() is invoked.
process
- the CSProcess to be addedAdd the array of processes to the Parallel object. The extended network will be executed the next time run() is invoked.
processes
- the CSProcesses to be addedStops all threads created by all Parallel and ProcessManager objects. No new threads can be created until the resetDestroy method gets called.
System finalizer. When this object falls out of scope it will release all of the threads that it has allocated.
Release all threads saved by the Parallel object for future runs - the threads all terminate and release their associated workspaces. This should only be executed when the Parallel object is not running. If this Parallel object is run again, the necessary threads will be recreated.
Remove all processes from the Parallel object. The cut-down network will not be executed until the next time run() is invoked.
Remove the process from the Parallel object. The cut-down network will not be executed until the next time run() is invoked.
process
- the CSProcess to be removedCancels a call to destroy allowing the JCSP system to be reused. This is provided to that destroy can be called from an Applet's termination method, but the Applet can be restarted later.
Run the parallel composition of the processes registered with this Parallel object. It terminates when, and only when, all its component processes terminate.
Implementation note: In its first run, only (numProcesses - 1) Threads are created to run the processes -- the last process is executed in the invoking Thread. Sunsequent runs reuse these Threads (so the overhead of thread creation happens only once).
Enables or disables the display or Errors uncaught by a CSProcess running within a Parallel or under a ProcessManager object.
Enables or disables the display of Exceptions uncaught by a CSProcess running within a Parallel or under a ProcessManager object.
JCSP for Java 1.8 generated 14-10-2016 by Jon Kerridge, Edinburgh Napier University - j dot kerridge at napier dot ac dot uk