|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.atlassian.util.concurrent.BlockingReference<V>
V
- the value type@ThreadSafe public class BlockingReference<V>
A Reference with queue semantics where the current reference may be retrieved
or taken instead, and if there is no current element then it will be block
until the reference becomes available. This is somewhat analogous to a single
element BlockingQueue
.
Note: this class does not support null elements being set(Object)
and will throw an exception. If the internal reference is null, then calls to
take()
or take(long, TimeUnit)
will block.
This class is most suited to SRSW
or MRSW
usage. Multiple writers will overwrite each other's elements and the
chosen value will be arbitrary in the absence of any external consensus. If
multiple readers are waiting to take()
a value, one reader will be
arbitrarily chosen (similar to Condition.signal()
). Multiple readers
can however get()
the current value if it is not null, but they may
see the current value more than once. If multiple readers attempt to
get()
a value from the SRSW reference and it is not yet present then
only one waiting thread may be notified, please use the MRSW version for this
case.
This implementation has been optimized for SRSW performance with
set(Object)
/take()
pairs.
Prometheus contains a similar construct called an AwaitableReference. This class is more explicit in that it handles take/get separately.
BlockingQueue
Constructor Summary | |
---|---|
BlockingReference()
Deprecated. use newSRSW() instead. |
|
BlockingReference(V value)
Deprecated. use newSRSW() instead. |
Method Summary | ||
---|---|---|
void |
clear()
Clear the current reference. |
|
V |
get()
Gets the current element if it is not null, if it is null then this method blocks and waits until it is not null. |
|
V |
get(long time,
java.util.concurrent.TimeUnit unit)
Gets the current element if it is not null, if it is null then this method blocks and waits until it is not null. |
|
boolean |
isEmpty()
Whether or not the current value is null or not. |
|
static
|
newMRSW()
Create a BlockingReference best suited to multi-reader/single-writer situations. |
|
static
|
newMRSW(V initialValue)
Create a BlockingReference best suited to multi-reader/single-writer situations. |
|
static
|
newSRSW()
Create a BlockingReference best suited to single-reader/single-writer situations. |
|
static
|
newSRSW(V initialValue)
Create a BlockingReference best suited to single-reader/single-writer situations. |
|
V |
peek()
Return the current value whether is null or not. |
|
void |
set(V value)
Set the value of this reference. |
|
V |
take()
Takes the current element if it is not null and replaces it with null. |
|
V |
take(long time,
java.util.concurrent.TimeUnit unit)
Takes the current element if it is not null and replaces it with null. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
@Deprecated public BlockingReference()
newSRSW()
instead.
@Deprecated public BlockingReference(@NotNull V value)
newSRSW()
instead.
Method Detail |
---|
public static <V> BlockingReference<V> newSRSW()
public static <V> BlockingReference<V> newSRSW(V initialValue)
initialValue
- the initial valuepublic static <V> BlockingReference<V> newMRSW()
public static <V> BlockingReference<V> newMRSW(V initialValue)
initialValue
- the initial value@NotNull public V take() throws java.lang.InterruptedException
If the current thread:
interrupted
while waiting,
InterruptedException
is thrown and the current thread's
interrupted status is cleared.
java.lang.InterruptedException
- if the current thread is interrupted while
waiting@NotNull public V take(long time, java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
TimeoutException
if the timeout is reached before an
element becomes available.
If the current thread:
interrupted
while waiting,
InterruptedException
is thrown and the current thread's
interrupted status is cleared.
time
- the maximum time to waitunit
- the time unit of the timeout
argument
java.lang.InterruptedException
- if the current thread is interrupted while
waiting
java.util.concurrent.TimeoutException
- if the timeout is reached without another thread
having called set(Object)
.@NotNull public V get() throws java.lang.InterruptedException
take()
it does not
reset the current element to null.
If the current thread:
interrupted
while waiting,
InterruptedException
is thrown and the current thread's
interrupted status is cleared.
java.lang.InterruptedException
- if the current thread is interrupted while
waiting@NotNull public V get(long time, @NotNull java.util.concurrent.TimeUnit unit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException
take()
it does not
reset the current element to null.
If the current thread:
interrupted
while waiting,
InterruptedException
is thrown and the current thread's
interrupted status is cleared.
java.util.concurrent.TimeoutException
- if the timeout is reached without another thread
having called set(Object)
.
java.lang.InterruptedException
- if the current thread is interrupted while
waitingpublic void set(@NotNull V value)
take()
or take(long, TimeUnit)
will be released and
given this value.
value
- the new value.public boolean isEmpty()
take()
or take(long, TimeUnit)
will not
block.
@Nullable public V peek()
take()
or take(long, TimeUnit)
will not
block.
public void clear()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |