Thread State

1.Control model example

    Thread state is stored in ActionContext, through which return, while, do, try/catch/final, continue, break and other grammatical models are implemented. Implementing the grammar class model is a more advanced content, which is seldom used by ordinary users. So how to write the grammar class action model is omitted here. If you are interested, you can refer to the implementation of the related model of XWorker.

    The GIF video below is an example of a single step action, showing the model of the grammar class and the context of the variables.

2.Relevant code for thread state in ActionContext

public class ActionContext implements Map<String, Object>{
	/** Normal operation status */
	public static final int RUNNING = 0;

	/** Returns the state of the value, returning to the place where the action was initially invoked */
	public static final int RETURN = 1;

	/** Cancel status, Cancel current action execution */
	public static final int CANCEL = 2;

	/** Interrupt status, usually returning to the previous loop */
	public static final int BREAK = 3;

	/** Continue to execute from the loop */
	public static final int CONTINUE = 4;

	/** The state of throwing an exception, usually to the end of exception handling */
	public static final int EXCEPTION = 5;

	public void setStatus(int status) {
		threadStatus.set(status);
	}

	public int getStatus() {
		Integer status = threadStatus.get();
		if(status == null){
			return RUNNING;
		}else{
			return status;
		}
	}
}

3.Interpretation Code of While Model

    The explanatory code for the While model is listed here, and other control models can be found in the source code of XWorker.

	public static Object run(ActionContext actionContext) throws Exception{
        Thing self = (Thing) actionContext.get("self");
        
        actionContext.peek().setVarScopeFlag();//.isVarScopeFlag = true;
        Object result = "success";        
        while(IfCreator.checkCondition(self, actionContext)){
        	Thing childActions = self.getThing("ChildAction@0"); 
        	for (Thing child : childActions.getChilds()) {
				Action action = world.getAction(child);
				if (action != null) {
					result = action.run(actionContext, null, true);
				}

				int sint = actionContext.getStatus();
				if (sint != ActionContext.RUNNING) {
					break;
				}
			}

			//Judging the state of the loop
			if (actionContext.getStatus() == ActionContext.BREAK) {
				actionContext.setStatus(ActionContext.RUNNING);
				break;
			} else if (actionContext.getStatus() == ActionContext.CONTINUE) {
				actionContext.setStatus(ActionContext.RUNNING);
				continue;
			} else if (actionContext.getStatus() != ActionContext.RUNNING) {
				break;
			}
        }
            
        return result;      
    }

Copyright ©  2007-2019 XWorker.org  版权所有  沪ICP备08000575号