-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CancellationTokenACancellationTokenwhich is already in the canceled state.static final CancellationControllerACancellationControllerwhich does nothing when callingcancel.static final CancellationTokenACancellationTokenwhich can never be in the canceled state. -
Method Summary
Modifier and TypeMethodDescriptionstatic CancellationTokenallTokens(CancellationToken... tokens) Returns aCancellationTokenwhich signals cancellation if and only if all of the specified tokens are in canceled state.static CancellationTokenanyToken(CancellationToken... tokens) Returns aCancellationTokenwhich signals cancellation if and only if at least one of the specified tokens are in canceled state.static CancellationSourceCreates a newCancellationSourcewhoseCancellationTokenis not yet in the canceled state.static CancellationSourcecreateChildCancellationSource(CancellationToken cancelToken) Creates a newCancellationSourcewhich will be notified of the cancellation requests of the specifiedCancellationToken.static <ResultType>
ResultTypedoAsCancelable(CancellationToken cancelToken, InterruptibleTask<ResultType> task) Executes the specified task on the current thread synchronously and interrupts the current thread if the specifiedCancellationTokensignals a cancellation request.static WaitableListenerReflistenForCancellation(CancellationToken cancelToken, Runnable listener) Adds acancellation listenerto the specifiedCancellationTokenand returns reference which can be used to remove the listener and wait until it has been removed.
-
Field Details
-
UNCANCELABLE_TOKEN
ACancellationTokenwhich can never be in the canceled state. TheisCanceledmethod ofUNCANCELABLE_TOKENwill always returnfalsewhen checked. If a listener is registered with this token to be notified of cancellation requests, thisCancellationTokenwill do nothing but return an already unregisteredListenerRef. -
CANCELED_TOKEN
ACancellationTokenwhich is already in the canceled state. TheisCanceledmethod ofUNCANCELABLE_TOKENwill always returntruewhen checked. If a listener is registered with this token to be notified of cancellation requests, thisCancellationTokenwill immediately notify the listener and return an already unregisteredListenerRef.If you need a
CancellationControlleras well, use theDO_NOTHING_CONTROLLER.- See Also:
-
DO_NOTHING_CONTROLLER
ACancellationControllerwhich does nothing when callingcancel.This
CancellationControlleris good for tasks, cannot be canceled or tasks already canceled.- See Also:
-
-
Method Details
-
createCancellationSource
Creates a newCancellationSourcewhoseCancellationTokenis not yet in the canceled state. The only possible way to make theCancellationTokenof the returnedCancellationSourcesignal cancellation request is to cancel theCancellationControllerof the returnedCancellationSource.- Returns:
- a new
CancellationSourcewhoseCancellationTokenis not yet in the canceled state. This method never returnsnull.
-
createChildCancellationSource
Creates a newCancellationSourcewhich will be notified of the cancellation requests of the specifiedCancellationToken. That is, if theCancellationTokenspecified in the argument is canceled, the returnedCancellationTokenwill be canceled as well. Of course, the returnedCancellationSourcecan also be canceled by its ownCancellationController- Parameters:
cancelToken- theCancellationToken, which when canceled, will cause the returnedCancellationSourceto be canceled. This argument cannot benull.- Returns:
- the new
ChildCancellationSourcewhich will be notified of the cancellation requests of the specifiedCancellationToken. This method never returnsnull. - Throws:
NullPointerException- thrown if the specified argument isnull
-
anyToken
Returns aCancellationTokenwhich signals cancellation if and only if at least one of the specified tokens are in canceled state.If you do not specify any tokens, the returned token will never be in the canceled state.
- Parameters:
tokens- the array ofCancellationTokenchecked for cancellation. This array might be empty but cannot containnullelements.- Returns:
- the
CancellationTokenwhich signals cancellation if and only if at least one of the specified tokens is in canceled state. This method never returnsnull.
-
allTokens
Returns aCancellationTokenwhich signals cancellation if and only if all of the specified tokens are in canceled state.If you do not specify any tokens, the returned token will always be in the canceled state.
- Parameters:
tokens- the array ofCancellationTokenchecked for cancellation. This array might be empty but cannot containnullelements.- Returns:
- the
CancellationTokenwhich signals cancellation if and only if all of the specified tokens is in canceled state. This method never returnsnull.
-
listenForCancellation
public static WaitableListenerRef listenForCancellation(CancellationToken cancelToken, Runnable listener) Adds acancellation listenerto the specifiedCancellationTokenand returns reference which can be used to remove the listener and wait until it has been removed.Warning: It is forbidden to call any of the
closemethods from within the added listener. Attempting to do so will result in anIllegalStateExceptionto be thrown by theclosemethod.Calling the
unregisterAndWaitmethod of the returned reference ensures the following:-
After the
unregisterAndWaitmethods returns normally (without throwing an exception), the listener is guaranteed not to be executed anymore. -
Calling the
unregisterAndWaitmethod (with valid argument) ensures that the added listener will be unregistered. This istrueeven if theunregisterAndWaitmethod gets canceled. -
If the passed listener is synchronization transparent, then
the
unregisterAndWaitmethod is synchronization transparent as well.
CancellationToken cancelToken = ...; WaitableListenerRef ref = listenerForCancellation(cancelToken, () -> { System.out.println("CANCELED") }); try { // ... } finally { ref.unregisterAndWait(Cancellation.UNCANCELABLE_TOKEN); } // When execution reaches this line, it is ensured that if "CANCELED" // has not been printed yet, it will never be printed.- Parameters:
cancelToken- theCancellationTokento which the listener is to be added. That is, the listener is registered to be notified of the cancellation requests of this token. This argument cannot benull.listener- the listener whoserunmethod is to be passed as a cancellation listener to the specifiedCancellationToken. This argument cannot benull.- Returns:
- a
CancelableCloseablewhoseclosemethods can be used to unregister the added listener and wait until it can be ensured that the listener will never be executed. This method never returnsnull. - Throws:
NullPointerException- thrown if any of the arguments isnull
-
After the
-
doAsCancelable
public static <ResultType> ResultType doAsCancelable(CancellationToken cancelToken, InterruptibleTask<ResultType> task) Executes the specified task on the current thread synchronously and interrupts the current thread if the specifiedCancellationTokensignals a cancellation request. TheInterruptedExceptionthrown by the specified task is treated as if the task has been canceled. That is,InterruptedExceptionthrown by the task is converted toOperationCanceledException; any other exception is simply propagated to the caller as it was thrown by the task.Note that this may cause the current thread to be interrupted without throwing any exception if the underlying task does not detect the thread interruption. The thread interrupted status will not be cleared even if it was caused by a cancellation request because there is no way to detect if outside code has also interrupted the current thread.
This method is intended to be used to convert code which uses thread interruption for cancellation to the more robust cancellation mechanism provided by JTrim.
- Type Parameters:
ResultType- the type of the result of the task to be executed- Parameters:
cancelToken- theCancellationTokenwhich when signals causes the current thread to be interrupted. This argument will also be passed to the task to be executed. This argument cannot benull.task- the task to be executed. This argument cannot benull.- Returns:
- the return value of the specified task. This value is exactly the
same object as the one returned by the task and so if the task returns
null, this method may also returnsnull. - Throws:
NullPointerException- thrown if any of the arguments isnullOperationCanceledException- thrown if the underlying task thrown anOperationCanceledExceptionor anInterruptedException. Note that the current thread will no be re-interrupted if the specified task throws anInterruptedException.
-