public final class CancellationToken
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static CancellationToken |
NONE
An empty CancellationToken that cannot be cancelled.
|
| Constructor and Description |
|---|
CancellationToken() |
| Modifier and Type | Method and Description |
|---|---|
void |
cancel()
Communicates a request for cancellation.
|
boolean |
isCancellationRequested()
Gets whether cancellation has been requested for this token by calling
cancel(). |
void |
registerOnCancel(java.lang.Runnable onCancel)
Registers a
Runnable that will be called when this CancellationToken is canceled. |
void |
registerOnCancel(java.lang.String id,
java.lang.Runnable onCancel)
Registers a
Runnable that will be called when this CancellationToken is canceled. |
void |
unregisterOnCancel(java.lang.String id)
Unregister the
Runnable that was registered using
registerOnCancel(String, Runnable). |
public static final CancellationToken NONE
public void cancel()
public boolean isCancellationRequested()
cancel().true if cancellation has been requested, otherwise.public void registerOnCancel(java.lang.Runnable onCancel)
Runnable that will be called when this CancellationToken is canceled.
This operation of registering the Runnable is non-blocking and thread-safe. If the token
is already in the cancelled state then the provided Runnable will be immediately executed.
This is O(1) in best case and O(k) in worst, where k is number of concurrent threads in case
of race.onCancel - The Runnable to be executed when the CancellationToken is canceled.public void registerOnCancel(java.lang.String id,
java.lang.Runnable onCancel)
Runnable that will be called when this CancellationToken is canceled.
This operation of registering the Runnable is non-blocking and thread-safe. If the token
is already in the cancelled state then the provided Runnable will be immediately executed.
This is O(1) in best case and O(k) in worst, where k is number of concurrent threads in case
of race in registering Callback.id - The registration id for the Runnable to register.onCancel - The Runnable to be executed when the CancellationToken is canceled.public void unregisterOnCancel(java.lang.String id)
Runnable that was registered using
registerOnCancel(String, Runnable).
This unregister operation is non-blocking and thread-safe.id - The id of the Runnable to unregister.