CancellationTokenSource (Gradle API 9.0.0)
Description: declaration: package: org.gradle.tooling, interface: CancellationTokenSource

Package org.gradle.tooling

# Interface CancellationTokenSource

public interface CancellationTokenSource
A `CancellationTokenSource` allows you to issue cancellation requests to one or more `LongRunningOperation` instances. To use a token source:

- Create a token source using `GradleConnector.newCancellationTokenSource()`.
- Attach the token to one or more operations using `LongRunningOperation.withCancellationToken(CancellationToken)`. You need to do this before you start the operation.
- Later, you can cancel the associated operations by calling `cancel()` on this token source.

All implementations of this interface are required to be thread safe.

Since:
  2.1

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `cancel()`
  Initiates cancel request.
  `CancellationToken`
  `token()`
  Returns a token associated with this `CancellationTokenSource`.

- ## Method Details

  - ### cancel

    void cancel()
    Initiates cancel request. All operations that have been associated with this token will be cancelled.

    It is assumed that the implementation will do 'best-effort' attempt to perform cancellation. This method returns immediately and if the cancellation is successful the cancelled operation will notify its `ResultHandler.onFailure(GradleConnectionException)` with a `BuildCancelledException` describing how it was cancelled.

  - ### token

    CancellationToken token()
    Returns a token associated with this `CancellationTokenSource`. Always returns the same instance.
    Returns:
      The cancellation token.