subsumption thread interruption (for-loop) question

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

subsumption thread interruption (for-loop) question

Postby rcly123 » Wed Aug 12, 2009 10:59 am

I have 2 behaviors being used in a lejos.subsumption example I'm working on:
MainBehavior, and
Interruptor

I also have an ExternalClass - being used by MainBehavior (see below).

The purpose of ExternalClass is to keep a counter, and every time MainBehavior calls one of it's methods the count gets incremented.

I have debugged this and ascertained that it is working.
Example Debug Output:
doing doX()
count is 1

doing doY()
count is 2

doing doY()
count is 3

etc.

My test case is this.

After doing 2 or 3 operations - touch the touch sensor, and Interruptor.action() should print the value of ExternalClass.count to be 2 or 3.
(i.e. If working = count should equal 2 or 3.)

The problem I'm getting is that count is equal to 14 (the size of the list), despite hitting the sensor after 2 or 3 operations (mid for-loop).

I'm wondering if the Arbitrator can't interrupt a for-loop, or somehow the for-loop is completing before Interruptor.action() is called?

---
Code Sample Below
---


class MainBehavior implements Behavior
{
...
ArrayList<String> list = filledList; //has been filled with 14 elements
ExternalClass myClass = new ExternalClass();// has been initialised
...
public void action()
{
for(String s: list) // will loop through 14 String elements of list
{
if(caseX)
{
myClass.doX()
myClass.addToCount();
}
else if (caseY)
{
myClass.doY()
myClass.addToCount();
}

}
}
}

class Interruptor implements Behavior
{
...
public boolean takeControl()
{
return touch.isPressed();
}

public void action()
{
LCD.drawString("sensor fired...",0,3);
Button.waitForPress();
Sound.beep();
LCD.clear();
LCD.drawInt(ExternalClass.getCount(), 0, 3);
}
}
}

class ExternalClass
{
private static int count=0;

public void addToCount()
{
count+=1;
}
public void refresh()
{
count=0;
}

public int getCount()
{
}
}

Anyone any ideas?
rcly123
New User
 
Posts: 1
Joined: Wed Aug 12, 2009 10:24 am

Postby gloomyandy » Wed Aug 12, 2009 12:11 pm

Hi,
The model used by the subsumption class is that once an action has been started it will run until completion. Alternative implementations would require multiple threads which would require a more complex structure (which was not considered appropriate for this class since subsumption mechanisms are supposed to be simple to use.). This means that if you have actions that are long running then you need to implement the ability to have them terminate early if the action is being suppressed. The normal way to do this is to add a flag to the Behavior class and set this in the Suppress method. The flag is usually cleared at the start of action method and then tested in any loops or other long running code. Once the suppressed flag is set the action routine should exit as quickly as possible.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3630
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff