Parallelism Next: AtomsUp: Basic InstructionsPrevious: Sequencing

Parallelism

The Merge  class extends BinaryInstruction and implements basic parallelism: at each instant the two instructions left and then right are activated in this order. It terminates when both left and right do terminate.

 
public class Merge extends BinaryInstruction
{
  private byte leftStatus = SUSP, rightStatus = SUSP;

  public Merge (Instruction left, Instruction right)
  {
     super.left = left;
     super.right = right;
  }

  public void reset(){ 
     super.reset(); leftStatus = rightStatus = SUSP; 
  }

  final public String toString(){ 
     return "(" + left + " || " + right + ")"; 
  }

  final protected byte activation(Machine machine)
  {
     if (leftStatus == SUSP) leftStatus = left.activ(machine);
     if (rightStatus == SUSP) rightStatus = right.activ(machine);
     if (leftStatus == TERM && rightStatus == TERM) return TERM;
     if (leftStatus == SUSP || rightStatus == SUSP) return SUSP;
     leftStatus = rightStatus = SUSP; 
     return STOP;
  }
}