Class Waiter
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidawait()Causes the current thread to wait until it is signalled or interrupted.longawaitNanos(long timeoutNS) Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.voidclear()Clears this waiter and release resources.getId()Returns the identification ornullif none defined.getValue()Returns the value of this waiter ornullif none available.booleanhasId()Returnstruethis waiter has an identification.voidinitialize(String id) Initialize this waiter for a new wait operation.voidlock()Acquires the lock.voidChanges the value of this waiter.voidsignal()Wakes up one waiting thread.voidWakes up all waiting threads.voidunlock()Attempts to release this lock.
-
Constructor Details
-
Waiter
public Waiter()Constructs a newWaiterinstance.
-
-
Method Details
-
clear
public void clear()Clears this waiter and release resources. -
initialize
Initialize this waiter for a new wait operation.- Parameters:
id- the new identification of the waiter.- Throws:
NullPointerException- ifidisnull.
-
hasId
public boolean hasId()Returnstruethis waiter has an identification.- Returns:
truethis waiter has an identification.
-
getId
Returns the identification ornullif none defined.- Returns:
- the identification or
nullif none defined.
-
getValue
Returns the value of this waiter ornullif none available.- Returns:
- the value of this waiter or
nullif none available.
-
setValue
Changes the value of this waiter.- Parameters:
value- the new value of this waiter.- Throws:
NullPointerException- ifvalueisnull.
-
lock
public void lock()Acquires the lock.Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one.
If the current thread already holds the lock then the hold count is incremented by one and the method returns immediately.
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired, at which time the lock hold count is set to one.
- See Also:
-
await
Causes the current thread to wait until it is signalled or interrupted.- Throws:
InterruptedException- if the current thread is interrupted.- See Also:
-
awaitNanos
Causes the current thread to wait until it is signalled or interrupted, or the specified waiting time elapses.- Parameters:
timeoutNS- the maximum time to wait (nanoseconds).- Returns:
- an estimate of the
timeoutNSvalue minus the time spent waiting upon return from this method. A positive value may be used as the argument to a subsequent call to this method to finish waiting out the desired time. A value less than or equal to zero indicates that no time remains. - Throws:
InterruptedException- if the current thread is interrupted.- See Also:
-
signal
public void signal()Wakes up one waiting thread.If any threads are waiting on this condition then one is selected for waking up. That thread must then re-acquire the lock before returning from
awaitNanos.- See Also:
-
signalAll
public void signalAll()Wakes up all waiting threads.If any threads are waiting on this condition then they are all woken up. Each thread must re-acquire the lock before it can return from
awaitNanos.- See Also:
-
unlock
public void unlock()Attempts to release this lock.If the current thread is the holder of this lock then the hold count is decremented. If the hold count is now zero then the lock is released. If the current thread is not the holder of this lock then
IllegalMonitorStateExceptionis thrown.- Throws:
IllegalMonitorStateException- if thread doesn't hold this lock.- See Also:
-