presto-main/src/main/java/io/prestosql/execution/StateMachine.java/StateMachine/addStateChangeListener
class StateMachine:    public void addStateChangeListener(StateChangeListener<T> stateChangeListener)
    {
        requireNonNull(stateChangeListener, "stateChangeListener is null");

        boolean inTerminalState;
        T currentState;
        synchronized (lock) {
            currentState = state;
            inTerminalState = isTerminalState(currentState);
            if (!inTerminalState) {
                stateChangeListeners.add(stateChangeListener);
            }
        }

        // fire state change listener with the current state
        // always fire listener callbacks from a different thread
        safeExecute(() -> stateChangeListener.stateChanged(currentState));
    }