boolean () { FormattingTask formattingTask = myTask; if (formattingTask != null && myStateRef.compareAndSet(FormattingRequestState.RUNNING, FormattingRequestState.CANCELLING)) { if (formattingTask.cancel()) { myStateRef.set(FormattingRequestState.CANCELLED); myTaskSemaphore.release(); return true; } } return false; }