Interface MpscIntQueue
- All Known Implementing Classes:
MpscIntQueue.MpscAtomicIntegerArrayQueue
public interface MpscIntQueue
A multi-producer (concurrent and thread-safe
offer and fill),
single-consumer (single-threaded poll and drain) queue of primitive integers.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classThis implementation is based on MpscAtomicUnpaddedArrayQueue from JCTools. -
Method Summary
Modifier and TypeMethodDescriptionstatic MpscIntQueuecreate(int size, int emptyValue) Create a new queue instance of the given size.intdrain(int limit, IntConsumer consumer) Remove up to the given limit of elements from the queue, and pass them to the consumer in order.intfill(int limit, IntSupplier supplier) Add up to the given limit of elements to this queue, from the given supplier.booleanisEmpty()Query if the queue is empty or not.booleanoffer(int value) Offer the given value to the queue.intpoll()Remove and return the next value from the queue, or return the "empty" value if the queue is empty.intsize()Query the number of elements currently in the queue.
-
Method Details
-
create
Create a new queue instance of the given size.Note: the size of the queue may be rounded up to nearest power-of-2.
- Parameters:
size- The required fixed size of the queue.emptyValue- The special value that the queue should use to signal the "empty" case. This value will be returned frompoll()when the queue is empty, and giving this value tooffer(int)will cause an exception to be thrown.- Returns:
- The queue instance.
-
offer
boolean offer(int value) Offer the given value to the queue. This will throw an exception if the given value is the "empty" value.- Parameters:
value- The value to add to the queue.- Returns:
trueif the value was added to the queue, orfalseif the value could not be added because the queue is full.
-
poll
int poll()Remove and return the next value from the queue, or return the "empty" value if the queue is empty.- Returns:
- The next value or the "empty" value.
-
drain
Remove up to the given limit of elements from the queue, and pass them to the consumer in order.- Parameters:
limit- The maximum number of elements to dequeue.consumer- The consumer to pass the removed elements to.- Returns:
- The actual number of elements removed.
-
fill
Add up to the given limit of elements to this queue, from the given supplier.- Parameters:
limit- The maximum number of elements to enqueue.supplier- The supplier to obtain the elements from.- Returns:
- The actual number of elements added.
-
isEmpty
boolean isEmpty()Query if the queue is empty or not.This method is inherently racy and the result may be out of date by the time the method returns.
- Returns:
trueif the queue was observed to be empty, otherwiseinvalid @code
{@code false.
-
size
int size()Query the number of elements currently in the queue.This method is inherently racy and the result may be out of date by the time the method returns.
- Returns:
- An estimate of the number of elements observed in the queue.
-