diff --git a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java index 05f2594c..93bd3990 100644 --- a/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java +++ b/spring-statemachine-core/src/main/java/org/springframework/statemachine/support/AbstractStateMachine.java @@ -111,6 +111,8 @@ public abstract class AbstractStateMachine extends StateMachineObjectSuppo private volatile Message forwardedInitialEvent; + private final Object lock = new Object(); + /** * Instantiates a new abstract state machine. * @@ -165,10 +167,12 @@ public abstract class AbstractStateMachine extends StateMachineObjectSuppo public State getState() { // if we're complete assume we're stopped // and state was stashed into lastState - if (lastState != null && isComplete()) { - return lastState; - } else { - return currentState; + synchronized (lock) { + if (lastState != null && isComplete()) { + return lastState; + } else { + return currentState; + } } } @@ -342,13 +346,15 @@ public abstract class AbstractStateMachine extends StateMachineObjectSuppo @Override protected void doStop() { - stateMachineExecutor.stop(); - notifyStateMachineStopped(buildStateContext(Stage.STATEMACHINE_STOP, null, null, this)); - // stash current state before we null it so that - // we can still return where we 'were' when machine is stopped - lastState = currentState; - currentState = null; - initialEnabled = null; + synchronized (lock) { + stateMachineExecutor.stop(); + notifyStateMachineStopped(buildStateContext(Stage.STATEMACHINE_STOP, null, null, this)); + // stash current state before we null it so that + // we can still return where we 'were' when machine is stopped + lastState = currentState; + currentState = null; + initialEnabled = null; + } } @Override