public final class QueuedSequenceSynchronizer extends Object
When a shared resource becomes available the signal()
method should
be called unconditionally.
A thread wishing to acquire a shared resource should:
currentSequence()
method waitUntilSequenceExceeded(long, long)
with that sequence. true
result from waitUntilSequenceExceeded(long, long)
,
the current sequence should again be obtained from the currentSequence()
method,
and an attempt to acquire the resource should be made. waitUntilSequenceExceeded(long, long)
with the previously obtained sequence. false
is received from waitUntilSequenceExceeded(long, long)
then a timeout has occurred.
When running on Java 8 and above, this class leverages the fact that when LongAdder
is monotonically increasing, and only LongAdder.increment()
and LongAdder.sum()
are used, it can be relied on to be Sequentially Consistent.
Constructor and Description |
---|
QueuedSequenceSynchronizer()
Default constructor
|
Modifier and Type | Method and Description |
---|---|
long |
currentSequence()
Get the current sequence.
|
int |
getQueueLength()
Returns an estimate of the number of threads waiting for a sequence threshold to be reached.
|
boolean |
hasQueuedThreads()
Queries whether any threads are waiting to for the sequence to reach a particular threshold.
|
void |
signal()
Signal any waiting threads.
|
boolean |
waitUntilSequenceExceeded(long sequence,
long nanosTimeout)
Block the current thread until the current sequence exceeds the specified threshold, or
until the specified timeout is reached.
|
public QueuedSequenceSynchronizer()
public void signal()
public long currentSequence()
public boolean waitUntilSequenceExceeded(long sequence, long nanosTimeout) throws InterruptedException
sequence
- the threshold the sequence must reach before this thread becomes unblockednanosTimeout
- a nanosecond timeout specifying the maximum time to waitInterruptedException
- if the thread is interrupted while waitingpublic boolean hasQueuedThreads()
public int getQueueLength()
Copyright © 2015 Zaxxer.com. All Rights Reserved.