Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.
public abstract class RealtimeTask
- Object
- RealtimeTask
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.
Constructors
public RealtimeTask() |
Methods
public void setup() | Runs once on the real-time thread, after its priority is applied and before the first period. |
public abstract void cycle() | One period’s work. |
public void periodsMissed(int count) | Called before cycle() when releases were missed because the previous cycle ran past them. |
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. |
public void stopped() | Called on the real-time thread after the last cycle, when RealtimeThread.stop() was called or after failed(Throwable). |
Inherited methods
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
countint- 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
failureThrowable- 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.