public interface CancellationToken
Defines an interface to detect cancellation requests. How cancellation is
done is implementation dependent but it is usually controlled by an
associated
CancellationController sharing the same
CancellationSource.
In general there are two ways to listen for cancellation requests:
-
The
isCanceled()method may be checked periodically and act accordingly when it returnstrue. Usually the best way to react to a cancellation request is to throw aOperationCanceledException. In this case the convenientcheckCanceled()method can be used. -
A listener maybe added to be notified immediately when cancellation occurs.
This can be done by calling the
addCancellationListener(Runnable)method. This asynchronous notification is usually preferable when a subtask (such as an IO operation) must be canceled as a response to a cancellation request .
Thread safety
The methods of this interface must be safe to be accessed by multiple threads concurrently.Synchronization transparency
TheisCanceled() and the checkCanceled() methods must be
synchronization transparent but the addCancellationListener
is not (as it may invoke cancellation listeners).-
Method Summary
Modifier and TypeMethodDescriptionaddCancellationListener(Runnable listener) Adds a listener which is to be called when cancellation was requested.default voidChecks if cancellation has been requested and throws aOperationCanceledExceptionif so otherwise returns immediately without doing anything.booleanReturnstrueif cancellation was requested.
-
Method Details
-
addCancellationListener
Adds a listener which is to be called when cancellation was requested. In case cancellation has already been requested, the listener may be notified on the current thread. Note that the listener may be notified in various places such as thecancelmethod ofCancellationControllerso it must not do anything expensive and especially must not wait for external events (such as IO).The specified listener will always be notified when cancellation is requested regardless when it occurred and it will not be notified multiple times.
Note that since this method may need to notify the listener on the current thread, it may propagate any unchecked exception the listener throws to the caller.
- Parameters:
listener- theRunnablewhoserunmethod is to be called when cancellation was requested. This listener can be notified in various places, so it is especially important that this listener does as little task as possible and must not wait for external events. This argument cannot benull.- Returns:
- the
ListenerRefwhich can be used to unregister the listener, so that it will no longer be notified of cancellation requests. Unregistering the listener prevents notifications of subsequent cancellation requests but if cancellation was requested prior to this call (or concurrently with this call), the listener might be notified even after the listener has been unregistered. This method never returnsnull.
-
isCanceled
boolean isCanceled()Returnstrueif cancellation was requested. This method may be checked periodically to detect cancellation requests but since the usual way to respond to such request is throwing aOperationCanceledException, it is more convenient to use thecheckCanceled()method.This method must be implemented so, that once it returned
true, it must returntrueforever after (it cannot revert to returningfalse).- Returns:
trueif cancellation was requested,falseif not
-
checkCanceled
default void checkCanceled()Checks if cancellation has been requested and throws aOperationCanceledExceptionif so otherwise returns immediately without doing anything.This method is only provided for convenience and it is a shorthand for the following code:
if (token.isCanceled()) { throw new OperationCanceledException(); }- Throws:
OperationCanceledException- thrown if cancellation was requested. If this exception is thrown,isCanceled()returnstrue.
-