Interface CancelObserver
- All Known Subinterfaces:
ProgressObserver
,SubProgressObserver
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 TypeMethodDescriptionboolean
Returns whether cancellation of current task has been requested.static CancelObserver
never()
Returns a cancel observer which won't cancel execution.static CancelObserver
onAnyCancelled
(CancelObserver... cancelObservers) Returns a cancel observer which cancels when any of the specified observers is cancelled.static CancelObserver
onTimeout
(long timeoutInSeconds) Returns a cancel observer which cancels after a specified timeout.
-
Method Details
-
never
Returns a cancel observer which won't cancel execution.- Returns:
- a null canceller which won't cancel
-
onTimeout
Returns a cancel observer which cancels after a specified timeout.This method returns a naive observer which checks
System.currentTimeMillis()
upon every invocation of itsisCancelled()
method.- Parameters:
timeoutInSeconds
- Timeout in seconds. Use0
for no timeout.- Returns:
- Canceller which cancel after a specified amount of time or a
never()
when0
timeout is specified
-
onAnyCancelled
Returns a cancel observer which cancels when any of the specified observers is cancelled.- Parameters:
cancelObservers
- Observers to watch. Note thatnull
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 atrue
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
-