public void run() { if (_serviceState == CREATED || _serviceState == STOPPED) { final STARTING state = new STARTING(); state.add(onCompleted); _serviceState = state; _start(new Task() { public void run() { _serviceState = STARTED; state.done(); } }); } else if (_serviceState instanceof STARTING) { ((STARTING) _serviceState).add(onCompleted); } else if (_serviceState == STARTED) { if (onCompleted != null) { onCompleted.run(); } } else { if (onCompleted != null) { onCompleted.run(); } error("start should not be called from state: " + _serviceState); } } });