Interface CancelObserver

All Known Subinterfaces:
ProgressObserver, SubProgressObserver

@PublicAPI public interface CancelObserver
Callback to propagate cancel requests into long-running tasks.

Thread-safety: implementations are not required to be thread-safe. Observed processes must be prepared for working with non-thread-safe observers. Consequently, observer methods can be called only on the thread that invoked the observed function. Alternatively, proper synchronization must be ensured by the observed code.

  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns whether cancellation of current task has been requested.
    Returns a cancel observer which won't cancel execution.
    onAnyCancelled(CancelObserver... cancelObservers)
    Returns a cancel observer which cancels when any of the specified observers is cancelled.
    onTimeout(long timeoutInSeconds)
    Returns a cancel observer which cancels after a specified timeout.
  • Method Details

    • never

      static CancelObserver never()
      Returns a cancel observer which won't cancel execution.
      Returns:
      a null canceller which won't cancel
    • onTimeout

      static CancelObserver onTimeout(long timeoutInSeconds)
      Returns a cancel observer which cancels after a specified timeout.

      This method returns a naive observer which checks System.currentTimeMillis() upon every invocation of its isCancelled() method.

      Parameters:
      timeoutInSeconds - Timeout in seconds. Use 0 for no timeout.
      Returns:
      Canceller which cancel after a specified amount of time or a never() when 0 timeout is specified
    • onAnyCancelled

      static CancelObserver onAnyCancelled(CancelObserver... cancelObservers)
      Returns a cancel observer which cancels when any of the specified observers is cancelled.
      Parameters:
      cancelObservers - Observers to watch. Note that null is accepted.
      Returns:
      Canceller which cancel when any of the passed non-null observers cancelled
    • isCancelled

      boolean isCancelled()
      Returns whether cancellation of current task has been requested.

      Long running/memory intensive operations should poll to see if cancellation has been requested. Since time-out based cancellations might be implemented polling should not be done too often.

      It is expected that implementations won't return false after a true value returned. It is also expected that implementations tolerate multiple calls regardless of the cancellation state.

      Returns:
      true if cancellation has been requested, false otherwise