V - the value type@ThreadSafe public class BlockingReference<V> extends Object
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.
This class is explicit in that it handles take/get separately.
BlockingQueue| Constructor and Description |
|---|
BlockingReference()
Deprecated.
use
newSRSW() instead. |
BlockingReference(V value)
Deprecated.
use
newSRSW() instead. |
| Modifier and Type | Method and Description |
|---|---|
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,
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 <V> BlockingReference<V> |
newMRSW()
Create a BlockingReference best suited to multi-reader/single-writer
situations.
|
static <V> BlockingReference<V> |
newMRSW(V initialValue)
Create a BlockingReference best suited to multi-reader/single-writer
situations.
|
static <V> BlockingReference<V> |
newSRSW()
Create a BlockingReference best suited to single-reader/single-writer
situations.
|
static <V> BlockingReference<V> |
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,
TimeUnit unit)
Takes the current element if it is not null and replaces it with null.
|
@Deprecated public BlockingReference()
newSRSW() instead.@Deprecated public BlockingReference(@NotNull V value)
newSRSW() instead.value - a V object.public static <V> BlockingReference<V> newSRSW()
BlockingReference object.public static <V> BlockingReference<V> newSRSW(V initialValue)
V - a V value to reference.initialValue - the initial valueBlockingReference.public static <V> BlockingReference<V> newMRSW()
BlockingReference.public static <V> BlockingReference<V> newMRSW(V initialValue)
V - a V value to reference.initialValue - the initial valueBlockingReference.@NotNull public final V take() throws InterruptedException
If the current thread:
interrupted while waiting,
InterruptedException is thrown and the current
thread's interrupted status is cleared.InterruptedException - if the current thread is interrupted
while waiting@NotNull public final V take(long time, TimeUnit unit) throws TimeoutException, 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 argumentInterruptedException - if the current thread is interrupted
while waitingTimeoutException - if the timeout is reached
without another thread having called set(Object).@NotNull public final V get() throws 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.InterruptedException - if the current thread is interrupted
while waiting@NotNull public final V get(long time, @NotNull TimeUnit unit) throws TimeoutException, 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.time - a long.unit - a TimeUnit.TimeoutException - if the timeout is reached
without another thread having called set(Object).InterruptedException - if the current thread is interrupted
while waitingpublic final void set(@NotNull
V value)
take() or take(long, TimeUnit) will be released and
given this value.value - the new value.public final boolean isEmpty()
take() or take(long, TimeUnit) will not
block.@Nullable public final V peek()
take() or take(long, TimeUnit) will not
block.public final void clear()
Copyright © 2016 Atlassian. All rights reserved.