Re: Execution jvm commands from java
shaposhnik wrote:
Patricia Shanahan ??????:
Patricia Shanahan wrote:
A State interface would probably be the most similar Java approach to this:
interface State{
State nextState([some parameters]);
}
with lots of references to subclass instances:
State someStateName = new State(){
State nextState([some parameters]){
do something;
if(some condition)
return someOtherState;
else
return yetAnotherState;
}
};
With a driving loop of the form:
State state = startState;
while(state != endState){
state = state.nextState(some parameters);
}
However, for a large monolithic state machine, this would end up with
hundreds of anonymous inner class files, one for each state.
Patricia
my top priority is speed, only than comes elegance. i fear that many
classes could result in excessive overhead.
Switches may be your best choice.
The concern I have with the single state machine and large switch is
that it leads to a pointer based indirect branch, for dense case
numbers, or a series of conditional branches with no particularly strong
take/not-take pattern, for the sparse case numbers.
The cost of a switch will depend on how well the processor's branch
prediction logic handles the patterns your loop produces. At the worst,
it could be loading the wrong stuff in the pipeline almost every
iteration, costing a cycle for each pipeline stage when it finds out
what the branch really did.
There is some hope that decomposing the problem into a series of smaller
switches would improve predictability for each conditional branch.
Patricia