Interface Buffer<E>
-
- Type Parameters:
E
- the type of elements maintained by this buffer
- All Known Implementing Classes:
BoundedBuffer
,BoundedBuffer.RingBuffer
,DisabledBuffer
,StripedBuffer
interface Buffer<E>
A multiple-producer / single-consumer buffer that rejects new elements if it is full or fails spuriously due to contention. Unlike a queue and stack, a buffer does not guarantee an ordering of elements in either FIFO or LIFO order.Beware that it is the responsibility of the caller to ensure that a consumer has exclusive read access to the buffer. This implementation does not include fail-fast behavior to guard against incorrect consumer usage.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <E> Buffer<E>
disabled()
Returns a no-op implementation.void
drainTo(java.util.function.Consumer<E> consumer)
Drains the buffer, sending each element to the consumer for processing.int
offer(E e)
Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.int
reads()
Returns the number of elements that have been read from the buffer.default int
size()
Returns the number of elements residing in the buffer.int
writes()
Returns the number of elements that have been written to the buffer.
-
-
-
Field Detail
-
FULL
static final int FULL
- See Also:
- Constant Field Values
-
SUCCESS
static final int SUCCESS
- See Also:
- Constant Field Values
-
FAILED
static final int FAILED
- See Also:
- Constant Field Values
-
-
Method Detail
-
disabled
static <E> Buffer<E> disabled()
Returns a no-op implementation.
-
offer
int offer(@Nonnull E e)
Inserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions. The addition is allowed to fail spuriously if multiple threads insert concurrently.- Parameters:
e
- the element to add- Returns:
1
if the buffer is full,-1
if the CAS failed, or0
if added
-
drainTo
void drainTo(@Nonnull java.util.function.Consumer<E> consumer)
Drains the buffer, sending each element to the consumer for processing. The caller must ensure that a consumer has exclusive read access to the buffer.- Parameters:
consumer
- the action to perform on each element
-
size
default int size()
Returns the number of elements residing in the buffer.- Returns:
- the number of elements in this buffer
-
reads
int reads()
Returns the number of elements that have been read from the buffer.- Returns:
- the number of elements read from this buffer
-
writes
int writes()
Returns the number of elements that have been written to the buffer.- Returns:
- the number of elements written to this buffer
-
-