Java usually employs thread interruption for canceling methods waiting for a
particular event. Since relying on thread interruption is error prone and
inconvenient to use, the JTrim library uses CancellationToken
to detect that an operation has been canceled. This library contains methods
to convert methods which can be canceled by thread interruption to a method
call which can be canceled using a CancellationToken.
The order of the arguments of these methods are always as follows:
-
The
CancellationToken. -
Optionally a timeout value and its
TimeUnitspecifying the maximum time to wait. - The object whose "await" method is to be called to wait for a particular event. The method of the object might not actually be called "await". Every method states in its documentation which method it actually calls.
The two general purpose method is which takes an InterruptibleWait
as their last argument and this InterruptibleWait must actually be
implemented to wait for the desired condition.
The methods of this class do add some additional performance overhead to the wait so it is advised to first check the condition to be waited for before actually calling a method of this class.
Warning: Every methods in this class will clear the interrupted status
of the task before they throw a OperationCanceledException. If they
return normally, they will leave the interrupted status of thread as it was.
Thread safety
Every method of this class can be called from multiple threads concurrently.Synchronization transparency
Every method of this class has the same synchronization transparency property as the "await" method of the object passed to them.-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanawait(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, Condition condition) Callscondition.await(long, TimeUnit)with the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled.static booleanawait(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, InterruptibleLimitedWait wait) Callswait.awaitwith the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled.static voidawait(CancellationToken cancelToken, Condition condition) Callscondition.await()and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled.static voidawait(CancellationToken cancelToken, InterruptibleWait wait) Waits untilwait.awaitreturns or the specifiedCancellationTokensignals that the waiting must be canceled.static booleanawaitTerminate(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, ExecutorService executor) Callsexecutor.awaitTermination(long, TimeUnit)with the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled.static voidlock(CancellationToken cancelToken, Lock lock) Callslock.lockInterruptibly()and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled.static voidsleep(CancellationToken cancelToken, long time, TimeUnit timeUnit) Causes the current thread to sleep until the given time elapses or the specifiedCancellationTokensignals a cancellation request.static booleantryLock(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, Lock lock) Callslock.tryLock(long, TimeUnit)with the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled.
-
Method Details
-
lock
Callslock.lockInterruptibly()and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, iflock.lockInterruptibly()throws anInterruptedException, thelock.lockInterruptibly()method will be called again.Whenever the specified
CancellationTokensignals a cancellation request, the ongoinglock.lockInterruptibly()call will be interrupted using thread interruption. Thelock.lockInterruptibly()call must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
lock.lockInterruptibly()calls may be interrupted spuriously. In such spurious interrupt will thelock.lockInterruptibly()method be called again.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.lock- theLockobject whoselockInterruptibly()method is to be invoked. This argument cannot benull.- Throws:
NullPointerException- thrown if any of the arguments isnullOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforelock.lockInterruptibly()returns
-
tryLock
public static boolean tryLock(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, Lock lock) Callslock.tryLock(long, TimeUnit)with the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, iflock.tryLock(long, TimeUnit)throws anInterruptedException, thelock.tryLock(long, TimeUnit)method will be called again with an appropriately lowered timeout value.Whenever the specified
CancellationTokensignals a cancellation request, the ongoinglock.tryLock(long, TimeUnit)call will be interrupted using thread interruption. Thelock.tryLock(long, TimeUnit)call must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
lock.tryLock(long, TimeUnit)calls may be interrupted spuriously. In such spurious interrupt will thelock.tryLock(long, TimeUnit)method be called again.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.timeout- the maximum time to wait in the given time unit. After this time elapses, this method returns by throwing aOperationCanceledException. This argument must be greater than or equal to zero.timeUnit- the time unit of thetimeoutargument. This argument cannot benull.lock- theLockobject whosetryLock(long, TimeUnit)method is to be invoked. This argument cannot benull.- Returns:
- the return value of the
lock.tryLock(long, TimeUnit)method call. The return value offalsemeans that the lock was not acquired. - Throws:
NullPointerException- thrown if any of the arguments isnullIllegalArgumentException- thrown if the specified timeout value is lower than zeroOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforelock.tryLock(long, TimeUnit)returns
-
sleep
Causes the current thread to sleep until the given time elapses or the specifiedCancellationTokensignals a cancellation request. If the specified time elapses, this method returns by throwing aOperationCanceledException.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this sleep must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.time- the time to wait in the given time unit. This argument must be greater than or equal to zero.timeUnit- the time unit of thetimeargument. This argument cannot benull.- Throws:
NullPointerException- thrown if any of the arguments isnullIllegalArgumentException- thrown if the specified time is lower than zeroOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request before the specified time elapses
-
awaitTerminate
public static boolean awaitTerminate(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, ExecutorService executor) Callsexecutor.awaitTermination(long, TimeUnit)with the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, ifexecutor.awaitTermination(long, TimeUnit)throws anInterruptedException, theexecutor.awaitTermination(long, TimeUnit)method will be called again with an appropriately lowered timeout value.Whenever the specified
CancellationTokensignals a cancellation request, the ongoingexecutor.awaitTermination(long, TimeUnit)call will be interrupted using thread interruption. Theexecutor.awaitTermination(long, TimeUnit)call must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
executor.awaitTermination(long, TimeUnit)calls may be interrupted spuriously. In such spurious interrupt will theexecutor.awaitTermination(long, TimeUnit)method be called again.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.timeout- the maximum time to wait in the given time unit. After this time elapses, this method returns by throwing aOperationCanceledException. This argument must be greater than or equal to zero.timeUnit- the time unit of thetimeoutargument. This argument cannot benull.executor- theExecutorServiceobject whoseawaitTermination(long, TimeUnit)method is to be invoked. This argument cannot benull.- Returns:
- the return value of the
executor.awaitTermination(long, TimeUnit)method call. The return value offalsemeans that the timeout elapsed without the executor terminating. - Throws:
NullPointerException- thrown if any of the arguments isnullIllegalArgumentException- thrown if the specified timeout value is lower than zeroOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforeexecutor.awaitTermination(long, TimeUnit)returns
-
await
public static boolean await(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, Condition condition) Callscondition.await(long, TimeUnit)with the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, ifcondition.await(long, TimeUnit)throws anInterruptedException, thecondition.await(long, TimeUnit)method will be called again with an appropriately lowered timeout value.Whenever the specified
CancellationTokensignals a cancellation request, the ongoingcondition.await(long, TimeUnit)call will be interrupted using thread interruption. Thecondition.await(long, TimeUnit)call must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
condition.await(long, TimeUnit)calls may be interrupted spuriously. In such spurious interrupt will thecondition.await(long, TimeUnit)method be called again.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.timeout- the maximum time to wait in the given time unit. After this time elapses, this method returns by throwing aOperationCanceledException. This argument must be greater than or equal to zero.timeUnit- the time unit of thetimeoutargument. This argument cannot benull.condition- theConditionobject whoseawait(long, TimeUnit)method is to be invoked. This argument cannot benull.- Returns:
- the return value of the
condition.await(long, TimeUnit)method call. The return value offalsemeans that the timeout elapsed without actually receiving the signal. - Throws:
NullPointerException- thrown if any of the arguments isnullIllegalArgumentException- thrown if the specified timeout value is lower than zeroOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforecondition.await(long, TimeUnit)returns
-
await
Callscondition.await()and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, ifcondition.await()throws anInterruptedException, thecondition.await()method will be called again.Whenever the specified
CancellationTokensignals a cancellation request, the ongoingcondition.await()call will be interrupted using thread interruption. Thecondition.await()call must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
condition.await()calls may be interrupted spuriously. In such spurious interrupt will thecondition.await()method be called again.Warning: Note that the
condition.await()method may return spuriously. That is, without any reason (as documented in its apidoc).- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.condition- theConditionobject whoseawait()method is to be invoked. This argument cannot benull.- Throws:
NullPointerException- thrown if any of the arguments isnullOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforecondition.await()returns
-
await
public static boolean await(CancellationToken cancelToken, long timeout, TimeUnit timeUnit, InterruptibleLimitedWait wait) Callswait.awaitwith the specified timeout value and waits until it returns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, ifwait.awaitthrows anInterruptedException, thewait.awaitmethod will be called again with an appropriately lowered timeout value.Whenever the specified
CancellationTokensignals a cancellation request, the ongoingwait.awaitcall will be interrupted using thread interruption. Thewait.awaitcall must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
wait.awaitcalls may be interrupted spuriously. In such spurious interrupt will thewait.awaitmethod be called again.This is a general purpose method for converting cancellation by thread interruption to cancellation by a
CancellationTokenand waiting for only a limited amount of time.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.timeout- the maximum time to wait in the given time unit. After this time elapses, this method returns by throwing aOperationCanceledException. This argument must be greater than or equal to zero.timeUnit- the time unit of thetimeoutargument. This argument cannot benull.wait- theInterruptibleWaitobject whoseawaitmethod is to be called to wait for a particular event. This argument cannot benull.- Returns:
- the return value of the
wait.awaitmethod call. Usually a return value offalsemeans that the timeout elapsed without the event to be waited for actually occurred. - Throws:
NullPointerException- thrown if any of the arguments isnullIllegalArgumentException- thrown if the specified timeout value is lower than zeroOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforewait.awaitreturns
-
await
Waits untilwait.awaitreturns or the specifiedCancellationTokensignals that the waiting must be canceled. That is, ifwait.awaitthrows anInterruptedException, thewait.awaitmethod will be called again.Whenever the specified
CancellationTokensignals a cancellation request, the ongoingwait.awaitcall will be interrupted using thread interruption. Thewait.awaitcall must return by throwing anInterruptedExceptionso that this method may clear the interrupted status of the current thread and return by throwing aOperationCanceledException.Note that thread interruption may occur due to reasons uncontrolled by this method, so
wait.awaitcalls may be interrupted spuriously. In such spurious interrupt will thewait.awaitmethod be called again.This is a general purpose method for converting cancellation by thread interruption to cancellation by a
CancellationToken.- Parameters:
cancelToken- theCancellationTokenwhich is to be checked if this operation must be canceled and aOperationCanceledExceptionshould be thrown. This argument cannot benull.wait- theInterruptibleWaitobject whoseawaitmethod is to be called to wait for a particular event. This argument cannot benull.- Throws:
NullPointerException- thrown if any of the arguments isnullOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforewait.awaitreturns
-