|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Thread lejos.subsumption.ActivityBase lejos.subsumption.Activity
public abstract class Activity
An activity that operates in coordination with other activities. Only one activity can run at a time. When an activity of a higher priority wants to run any activities of a lower priority will be suppressed. Furthermore, if there is already an activity of the same or lower priority running, that activity will be stopped.
This is essentially an alternative way of implementing a Behavior or Subsumption architecture. It is totally distinct from the Behavior and Arbitrator classes and does not use either.
It works like this:
Example:
/** * When sensor one is pressed, run the motors in some pattern. */ class ControlMotors extends Activity implements PortListener { public ControlMotors() { Port.S1.addPortListener(this); } /** * Called when the sensor state changes (in some thread other than this one). */ public void stateChanged(Port s, int old, int nu) { if (old > nu) return; iWantToRun(); } /** * Encapsulates the actual activity we want to perform. * * @exception StopException if we are forcibly stopped. */ protected void action() throws StopException { boolean finished = false; // Sit here until we are finished or we are forcibly halted. while (!finished) { try { // spin Motor.C.forward(); Motor.A.backward(); // Wait for 0.25 secs, may throw InterruptedException pause(250); // Forward Motor.A.forward(); // We are finished finished = true; } catch (InterruptedException ie) { // pause() was interrupted. Re-start from the beginning } } } }
Field Summary |
---|
Fields inherited from class lejos.subsumption.ActivityBase |
---|
monitor |
Fields inherited from class java.lang.Thread |
---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
Constructor Summary | |
---|---|
Activity()
|
Method Summary | |
---|---|
protected abstract void |
action()
Encapsulates the actual activity we want to perform. |
protected void |
iWantToRun()
Call this if you want this activity to run. |
protected void |
pause()
Wait until we've either been made runnable or someone else has. |
protected void |
pause(long time)
Wait at most 'time' milliseconds. |
protected void |
resetRunnable()
Reset the runnable activity. |
void |
run()
Thread entry point. |
Methods inherited from class java.lang.Thread |
---|
currentThread, getPriority, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, setDaemon, setPriority, sleep, start, yield |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Activity()
Method Detail |
---|
protected abstract void action() throws StopException
StopException
- when some other activity has stopped this one.protected void resetRunnable()
protected final void iWantToRun()
public final void run()
run
in interface Runnable
run
in class Thread
protected final void pause(long time) throws InterruptedException, StopException
InterruptedException
- if we were made runnable whilst
we were running. This might indicate, for example, that a sensor
was pressed while we were still reacting to an earlier press.
Typically, an activity would want to restart execution of the
action() method from the beginning if that happened.
StopException
- if we should stop executing altogether.protected final void pause() throws InterruptedException, StopException
InterruptedException
- if we should restart execution of the
action() method from the beginning.
StopException
- if we should stop executing altogether.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |