How to Code a Cyclic Executive in Java

A cyclic executive provides an alternative to writing a full-blown, real-time operating system. It consists of an infinite loop that contains sets of actions. Each set is preceded by a wait period that controls when the set is to perform its actions. The following code provides an example:

while(true)
{
//wait 3 seconds
wait(3000);
// receive a network message
.......
// wait 2 seconds for complete reception of the message
wait(2000);
// scan the contents of the message
.......
}

These action sets can often run concurrently with some degree of synchronization. In Java, threads are a mechanism for running concurrent tasks using pauses to assist in synchronization.

Instructions

  1. Identify Tasks

    • 1

      Select a design pattern that is closest to the behavior requirements of the application to be built as a cyclic executive. Make sure that the design pattern covers the content of the loop making up the cyclic executive.

    • 2

      Pair the behaviors of the design pattern with the application requirements. Make sure that each of the synchronized, cooperating processes is associated with each of the behaviors described in the application.

    • 3

      Identify the Java-specific details that make up each of the process Thread classes of the application. Make sure to set up a synchronization pause for each process Thread class to assure proper timing between processes and avoid anomalous conditions such as racing. Create support classes to act as data exchange buffers between processes, such as queues, as needed. Create a log stream class to define logs tracing the execution of each process thread.

    Organize the Package

    • 4

      Build a cyclic executive main class that instantiates the process Thread classes, special support classes and any other required initialization, such as the following:

      class CyclicExec
      {
      public static void main(String [] args)
      {
      // initialize needed resources
      .....................................
      // define a thread log class
      LogStream ls = new LogStream();
      .....................................
      // initialize support classes
      .....................................
      MsgQueue sq = new MsgQueue(....);
      .....................................
      // initialize process thread classes
      .....................................
      // instantiate the Process_1 process and define its associated thread
      Process_1 p1 = new Process_1(......);
      T_Process_1 = new Thread(p1);
      // activate the thread for Process_1
      try
      {
      T_Process_1.start();
      }
      catch(IllegalThreadStateException e)
      {
      // log a bad thread start and exit
      .....................................
      }
      }
      }

    • 5

      Add to the package each of the process Thread classes, as the following example shows:

      class Process_1 extends Thread
      {
      // initialization
      public Process_1(....)
      {
      // set up a log for this thread (p_s is pre-defined log stream object)
      PrintStream p1_s = p_s.make_it(Processs_1_Log_File);
      // initialize the process thread
      .....................................
      }
      .....................................
      // execution - prompted through the implicit start() method used in the main class
      public void run()
      {
      // core loop
      while(true)
      {
      // core process
      .....................................
      // pause after running one cycle of the core process
      try
      {
      sleep(Process_1_Wait);
      }
      catch(InterruptedException e)
      {
      // log a pause exception
      .......................................
      }
      }
      }
      }

    • 6

      Build the support classes that define messaging buffers between process Threads with its buffer access methods qualified as synchronized, as the following example shows:

      public synchronized byte PushIt(byte Itm)
      {
      .............................................................
      }

    • 7

      Build a special class that defines the log files tracing the behavior of the process Thread classes, such as:

      class LogStream
      {
      private boolean LogState;
      // class initializer
      public LogStream(String LogStateIn)
      {
      this.LogState = false;
      if(LogStateIn.equals("yes")) this.LogState = true;
      }
      // create a log stream to a file
      public PrintStream make_it(String LogLocation)
      {
      PrintStream This1;
      String This2;
      if(this.LogState)
      {
      This2 = new String(LogLocation);
      }
      else
      {
      This2 = new String("/dev/null");
      }
      try
      {
      This1 = new PrintStream(new FileOutputStream(This2,true));
      }
      catch(Exception e)
      {
      This1 = null;
      }
      return This1;
      }
      }

    Test and Deliver

    • 8

      Define a sequence of test scenarios to cover both extreme and normal operation conditions in order to refine the synchronization between the process threads.

    • 9

      Review the process thread logs to make sure that the process Thread classes operated as expected. Make sure that no racing or similar conditions occurred.

    • 10

      Prepare the application delivery package by including the class (executable) files for each of the process threads, the main class, messaging support classes and log stream class. Include scripts that unload and set up the class files as well as initialize the directory where process thread log files are to be located.

Tips & Warnings

  • Before building a full application and after identifying the process threads, create a set of skeleton process classes and main class. These classes have only the pause settings for each process thread class. Use them to test the performance of the proposed design in a target environment -- in setting initial synchronized pause periods, for example. You can refine them later when running the test scenarios for the full application.

Related Searches:

References

Resources

Comments

Related Ads

Featured