Class RealtimeTask

java.lang.Object
com.codename1.realtime.RealtimeTask

public abstract class RealtimeTask extends Object

The work a RealtimeThread runs every period.

class MotorLoop extends RealtimeTask {
    private PwmChannel pwm;
    private final byte[] reading = new byte[2];
    private I2cDevice encoder;

    public void setup() {
        // runs once on the real-time thread; may allocate
        ...
    }

    public void cycle() {
        // every period; must not allocate
        encoder.read(reading, 0, 2);
        pwm.setDutyCycle(controlLaw(reading));
    }
}

cycle() and periodsMissed(int) must not allocate: no new, no boxing, no string concatenation, no iterator from a for-each over a collection. Keep buffers and objects in fields, made in the constructor or in setup(). The build checks every method they reach through calls it can resolve and fails with the call chain; at run time an allocation the build could not see ends the steady state with a RealtimeAllocationError, unless the thread has an allocation budget.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract void
    One period's work.
    void
    failed(Throwable failure)
    Called on the real-time thread when the task ends abnormally: setup() or cycle() threw, or the thread could not be made real-time as its parameters require.
    void
    periodsMissed(int count)
    Called before cycle() when releases were missed because the previous cycle ran past them.
    void
    Runs once on the real-time thread, after its priority is applied and before the first period.
    void
    Called on the real-time thread after the last cycle, when RealtimeThread.stop() was called or after failed(Throwable).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RealtimeTask

      public RealtimeTask()
  • Method Details

    • setup

      public void setup()
      Runs once on the real-time thread, after its priority is applied and before the first period. It may allocate: create here what the cycles will use, and touch every class they use so its static initializer has run.
    • cycle

      public abstract void cycle()
      One period's work. Runs on the real-time thread at every release and must not allocate.
    • periodsMissed

      public void periodsMissed(int count)

      Called before cycle() when releases were missed because the previous cycle ran past them. Missed releases are skipped, not run late: the cycle that follows is the one due now. Must not allocate. The default does nothing.

      Parameters
      • count: how many releases were skipped
    • failed

      public void failed(Throwable failure)

      Called on the real-time thread when the task ends abnormally: setup() or cycle() threw, or the thread could not be made real-time as its parameters require. The steady state is over when this runs, so it may allocate. The thread stops afterwards. The default prints the stack trace.

      Parameters
      • failure: what ended the task
    • stopped

      public void stopped()
      Called on the real-time thread after the last cycle, when RealtimeThread.stop() was called or after failed(Throwable). May allocate. The default does nothing.