Class WaitableSignal
WaitableSignal is in the non-signaled state but after invoking
the signal() method, it will permanently enter the signaled state.
Other threads can wait for this signal by calling the
waitSignal or the
tryWaitSignal
method.
Note that this class is similar to a
java.util.concurrent.CountDownLatch with one as the initial "count"
but this implementation is simpler and relies on
OperationCanceledException rather than on thread
interrupts.
Thread safety
The methods of this class are safe to use by multiple threads concurrently.Synchronization transparency
The methods of this interface are not synchronization transparent.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final WaitableSignalAWaitableSignalwhich is already in the signaling state. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidsignal()Sets the state of thisWaitableSignalto the signaled state and allows thewaitSignalor thetryWaitSignalmethod to return immediately.booleantryWaitSignal(CancellationToken cancelToken, long timeout, TimeUnit timeUnit) Waits until another thread invokes thesignal()method or until the specifiedCancellationTokensignals a cancellation request or until the specified timeout elapses.voidwaitSignal(CancellationToken cancelToken) Waits until another thread invokes thesignal()method or until the specifiedCancellationTokensignals a cancellation request.
-
Field Details
-
SIGNALING_SIGNAL
AWaitableSignalwhich is already in the signaling state. That is, calling any of its wait method is effectively a no-op.
-
-
Constructor Details
-
WaitableSignal
public WaitableSignal()Creates a newWaitableSignalin the non-signaled state.
-
-
Method Details
-
signal
public void signal()Sets the state of thisWaitableSignalto the signaled state and allows thewaitSignalor thetryWaitSignalmethod to return immediately.This method is idempotent. That is, calling this method multiple times has no further effect.
-
isSignaled
public boolean isSignaled()Returnstrueifsignal()has been already called on thisWaitableSignalobject.If this method returns
true, subsequentwaitSignalortryWaitSignalmethod calls will immediately return without waiting (or throwing an exception).- Returns:
trueifsignal()has been already called on thisWaitableSignalobject,falseotherwise
-
waitSignal
Waits until another thread invokes thesignal()method or until the specifiedCancellationTokensignals a cancellation request.Note that if the
signal()method has been called prior to thiswaitSignalmethod call, this method will always return immediately without throwing an exception (even if theCancellationTokensignals a cancellation request).- Parameters:
cancelToken- theCancellationTokenwhich is to be checked for cancellation request. A cancellation request will cause this method to throw anOperationCanceledExceptionexception. This argument cannot benull.- Throws:
NullPointerException- thrown if the specifiedCancellationTokenisnullOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforesignal()has been called.
-
tryWaitSignal
Waits until another thread invokes thesignal()method or until the specifiedCancellationTokensignals a cancellation request or until the specified timeout elapses.Note that if the
signal()method has been called prior to thistryWaitSignalmethod call, this method will always return withtrueimmediately without throwing an exception (even if theCancellationTokensignals a cancellation request).- Parameters:
cancelToken- theCancellationTokenwhich is to be checked for cancellation request. A cancellation request will cause this method to throw anOperationCanceledExceptionexception. This argument cannot benull.timeout- the maximum time to wait for the signal in the given time unit before returning. If the timeout elapses, this method will return withfalse. This argument must be greater than or equal to zero.timeUnit- the time unit of thetimeoutargument. This argument cannot benull- Returns:
trueif this method has detected that thesignal()method has been called,falseif the specified timeout elapsed first.- Throws:
NullPointerException- thrown if the specifiedCancellationTokenorTimeUnitisnullOperationCanceledException- thrown if the specifiedCancellationTokensignals a cancellation request beforesignal()has been called.
-