public class ProcessManager
This enables a CSProcess to be spawned concurrently with the process doing the spawning.
Shortcut to the Constructor and Method Summaries.
Spawning processes is not the normal way of creating a network in JCSP - the normal method is to use the Parallel class. However, when we need to add processes in response to some run-time event, this capability is very useful.
For completeness, ProcessManager is itself a CSProcess - running a ProcessManager simply runs the process it is managing.
import org.jcsp.lang.*; public class ProcessManagerExample1 { public static void main (String[] argv) { final ProcessManager manager = new ProcessManager ( new CSProcess () { public void run () { final CSTimer tim = new CSTimer (); long timeout = tim.read (); int count = 0; while (true) { System.out.println (count + " :-) managed process running ..."); count++; timeout += 100; tim.after (timeout); // every 1/10th of a second ... } } } ); final CSTimer tim = new CSTimer (); long timeout = tim.read (); System.out.println ("\n\n\t\t\t\t\t *** start the managed process"); manager.start (); for (int i = 0; i < 10; i++) { System.out.println ("\n\n\t\t\t\t\t *** I'm still executing as well"); timeout += 1000; tim.after (timeout); // every second ... } System.out.println ("\n\n\t\t\t\t\t *** I'm finishing now!"); } }*
To avoid this, a managed process should only be stopped if we know it is in a state where it is not interacting with its environment - or, as in the above example, we do not care about such spoilt data. To know that it is in a stoppable state, the manager ought to listen for a message (e.g. channel communication) from the process that it is ready to be stopped. [An example of such cooperation between managed and manager processes is given in the Hamming demonstration (see jcsp-demos.hamming).]
If the managed process is purely serial, there is not much point in the above trick, since it could simply terminate. However, having a manager stop a complex process network means that the network does not have to make its own provision for termination. The latter can add considerable algorithmic complexity, often to the detriment of the clarity of individual process logic.
Stopping a network by setting a global volatile flag that each process polls from time to time (the first suggestion in the documentation of the deprecated stop method in java.lang.Thread) is not, in general, safe. For example, a thread blocked on a monitor wait will remain blocked if the thread that was going to notify it spots the shut-down flag and terminates. The JDK1.2 documentation describes some work-arounds, but they are not simple and depend in part on the application logic.
For JCSP processes, there is a general solution to this [`Graceful Termination and Graceful Resetting', P.H.Welch, Proceedings of OUG-10, pp. 310-317, Ed. A.W.P.Bakkers, IOS Press (Amsterdam), ISBN 90 5199 011 1, April, 1989], based on the careful distribution of poison over the network's normal communication channels. Future versions of JCSP may take account of this.
Modifiers | Name | Description |
---|---|---|
static int |
PRIORITY_MAX |
The maximum priority value for running a process. |
static int |
PRIORITY_MIN |
The minimum priority value for running a process. |
static int |
PRIORITY_NORM |
The normal priority value for running a process. |
Constructor and description |
---|
ProcessManager
(CSProcess proc) @param proc the CSProcess to be executed by this ProcessManager |
Type | Name and description |
---|---|
int |
getPriority() |
void |
interrupt() Interrupt the managed process. |
void |
join() Join the managed process (that is wait for it to terminate). |
void |
run() |
void |
setPriority(int priority) |
void |
start() Start the managed process (but keep running ourselves). |
void |
start(int priority) Start the managed process at a specified priority (but keep running ourselves). |
void |
stop() Stop (permanently) the managed process. |
The maximum priority value for running a process.
The minimum priority value for running a process.
The normal priority value for running a process.
Public accessor for obtaining the ProcessManager
object's
process' priority.
ProcessManager
object's
process will be run.Interrupt the managed process. This will usually cause the process to throw a ProcessInterruptedException, which will likely halt the process.
Join the managed process (that is wait for it to terminate).
Run the managed process (that is start it and wait for it to terminate).
This will adjust the priority of the calling process to the priority of
this ProcessManager
and then return the priority to the
previous value once the managed process has terminated.
The managed process can be run at the caller's priority simply by directly
calling the CSProcess
object's run()
method.
Public mutator for setting the ProcessManager
object's
process' priority.
The priority should be specified as an int
between
PRIORITY_MIN
and
PRIORITY_MAX
.
the
- priority to use.Start the managed process (but keep running ourselves).
Start the managed process at a specified priority
(but keep running ourselves). The priority of the
ProcessManager
that this is called upon
will remain at the specified priority once the process
has terminated.
The priority should be specified as an int
between
PRIORITY_MIN
and
PRIORITY_MAX
.
priority
- the priority at which to start the process.
Stop (permanently) the managed process. This method now calls interrupt(), which will not always stop the process.
JCSP for Java 1.8 generated 14-10-2016 by Jon Kerridge, Edinburgh Napier University - j dot kerridge at napier dot ac dot uk