Class Waiter<T>
- Type Parameters:
T- the type of value delivered to the waiting thread.
A thread calls await(long) to block until another thread provides a
value through signal(String, Object) with a matching identification.
This class is typically used to coordinate request–response interactions
between threads, such as in asynchronous I/O or messaging systems.
This class is designed for a single waiting thread at a time. Multiple threads waiting on the same Waiter instance are not supported.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionawait(long timeout) Waits until this waiter is completed or the specified timeout expires.voidcancel()Cancels this waiter and wakes the waiting thread, if any.voidclear()Resets this waiter, discarding the current identification and value.voidinitialize(String id) Initializes this waiter for a new waiting operation.booleanDelivers a value to the waiting thread and marks this waiter completed.booleantryClaim()Atomically claims the waiter for a new request if not already in use.
-
Constructor Details
-
Waiter
public Waiter()
-
-
Method Details
-
initialize
Initializes this waiter for a new waiting operation.The specified
ididentifies the request associated with this waiter. Any previously stored value is cleared and the completion state is reset so that a subsequent call toawait(long)will block until a matching call tosignal(String, Object)occurs.- Parameters:
id- the identification associated with the next wait operation.- Throws:
NullPointerException- ifidisnull.
-
await
Waits until this waiter is completed or the specified timeout expires.If
timeoutis0, this method waits indefinitely until completion.If the waiter has not been completed, the current thread is suspended until another thread invokes
signal(String, Object)with a matching identification, the waiter is cancelled, or the timeout expires.This method is responsive to interruption. If the current thread is interrupted while waiting, an
InterruptedExceptionis thrown and the interrupted status is cleared.- Parameters:
timeout- the max time to wait (millisecs) (0 = wait indefinitely).- Returns:
- the delivered value or
nullif timeout or cancelled. - Throws:
IllegalArgumentException- iftimeoutis negative.InterruptedException- if current thread interrupted while waiting.
-
signal
Delivers a value to the waiting thread and marks this waiter completed.If the specified
idmatches the current waiter identification, the value is stored and the waiting thread (if any) is unparked.Only the thread currently waiting via
await(long)will be unparked. If no thread is waiting, the value is stored and will be returned to the next call toawait(long).- Parameters:
id- the identification expected by this waiter.value- the value to deliver to the waiting thread.- Returns:
trueif the waiter was successfully signalled;falseif the specifiediddoes not match the current waiter identification.- Throws:
NullPointerException- if an argument isnull.
-
tryClaim
public boolean tryClaim()Atomically claims the waiter for a new request if not already in use.- Returns:
- true if claim succeeded, false if another request is running.
-
cancel
public void cancel()Cancels this waiter and wakes the waiting thread, if any.Once cancelled,
await(long)will returnnullimmediately. -
clear
public void clear()Resets this waiter, discarding the current identification and value.This does not affect threads currently blocked in
await(long). It is intended to prepare the waiter for a new operation.
-