Class CancellationTokenSource


  • public class CancellationTokenSource
    extends Object
    This class provides functionality to control the execution of promises. It acts as a source for cancellation tokens.

    The example below demonstrates how to retrieve a CancellationToken and use it to cancel a pending operation.

     CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
     CancellationToken ct = cancellationTokenSource.getToken();
    
     // Pass the token to an asynchronous operation that accepts a CancellationToken
     asyncOperation(..., ct).conclude();
    
     // If required, cancellation can be requested as follows:
     cancellationTokenSource.requestCancellation();
     
    See Also:
    CancellationToken, Promise
    • Constructor Detail

      • CancellationTokenSource

        public CancellationTokenSource()
        Creates a new cancellation token source.
      • CancellationTokenSource

        public CancellationTokenSource​(boolean cancellationRequested)
        Creates a new cancellation token source with cancellation already requested. This eliminates the need to call requestCancellation() after instantiation if cancellation is to be requested at instantiation time.
        Parameters:
        cancellationRequested - true, if cancellation is to be requested and false, if not.
    • Method Detail

      • requestCancellation

        public void requestCancellation()
        Requests cancellation of the affected promises.
      • requestCancellation

        public void requestCancellation​(String message)
        Requests cancellation of the affected promises and allows specifying a custom message.