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.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <E> Buffer<E> disabled()Returns a no-op implementation.voidDrains the buffer, sending each element to the consumer for processing.intInserts the specified element into this buffer if it is possible to do so immediately without violating capacity restrictions.intreads()Returns the number of elements that have been read from the buffer.default intsize()Returns the number of elements residing in the buffer.intwrites()Returns the number of elements that have been written to the buffer.
-
Field Details
-
FULL
static final int FULL- See Also:
-
SUCCESS
static final int SUCCESS- See Also:
-
FAILED
static final int FAILED- See Also:
-
-
Method Details
-
disabled
Returns a no-op implementation. -
offer
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:
1if the buffer is full,-1if the CAS failed, or0if added
-
drainTo
-
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
-