public boolean cancel(boolean setFlag) {
        final AtomicInteger stateRef = this.stateRef;
        int oldVal, newVal;
        do {
            oldVal = stateRef.get();
            if (oldVal == ST_WAITING) {
                newVal = ST_CANCELLED;
            } else if (oldVal == ST_CANCELLED) {
                if (! setFlag) {
                    return true;
                }
                newVal = ST_CANCELLED_FLAG_SET;
            } else if (oldVal == ST_CANCELLED_FLAG_SET) {
                // do nothing
                return true;
            } else if (oldVal == ST_STARTED) {
                if (! setFlag) {
                    return false;
                }
                newVal = ST_STARTED_FLAG_SET;
            } else {
                assert oldVal == ST_STARTED_FLAG_SET;
                return false;
            }
        } while (! stateRef.compareAndSet(oldVal, newVal));
        return newVal == ST_CANCELLED || newVal == ST_CANCELLED_FLAG_SET;
    }
public boolean cancel ( boolean setFlag ) { final AtomicInteger stateRef = this . stateRef ; int oldVal , newVal ; do { oldVal = stateRef . get ( ) ; if ( oldVal == ST_WAITING ) { newVal = ST_CANCELLED ; } else if ( oldVal == ST_CANCELLED ) { if ( ! setFlag ) { return true ; } newVal = ST_CANCELLED_FLAG_SET ; } else if ( oldVal == ST_CANCELLED_FLAG_SET ) { return true ; } else if ( oldVal == ST_STARTED ) { if ( ! setFlag ) { return false ; } newVal = ST_STARTED_FLAG_SET ; } else { assert oldVal == ST_STARTED_FLAG_SET ; return false ; } } while ( ! stateRef . compareAndSet ( oldVal , newVal ) ) ; return newVal == ST_CANCELLED || newVal == ST_CANCELLED_FLAG_SET ; }
Attempt to cancel the corresponding invocation.

@param setFlag {@code true} to set the EJB context cancellation flag (or equivalent), {@code false} otherwise
@return {@code true} if the invocation was definitely cancelled, or {@code false} if it was not cancelled or it could not be determined if it was cancelled