public virtual bool Cancel(bool mayInterruptIfRunning) { if (!(State == NEW && UNSAFE.compareAndSwapInt(this, StateOffset, NEW, mayInterruptIfRunning ? INTERRUPTING : CANCELLED))) { return(false); } try // in case call to interrupt throws exception { if (mayInterruptIfRunning) { try { Thread t = Runner; if (t != null) { t.Interrupt(); } } // final state finally { UNSAFE.putOrderedInt(this, StateOffset, INTERRUPTED); } } } finally { FinishCompletion(); } return(true); }
/// <summary> /// Sets the result of this future to the given value unless /// this future has already been set or has been cancelled. /// /// <para>This method is invoked internally by the <seealso cref="#run"/> method /// upon successful completion of the computation. /// /// </para> /// </summary> /// <param name="v"> the value </param> protected internal virtual void Set(V v) { if (UNSAFE.compareAndSwapInt(this, StateOffset, NEW, COMPLETING)) { Outcome = v; UNSAFE.putOrderedInt(this, StateOffset, NORMAL); // final state FinishCompletion(); } }