Removes all items from the queue.
Removes all items from the queue.
Called from the consumer thread, subject to the restrictions appropriate to the implementation indicated by ChannelType.
WARNING: the clear
operation should be done on the consumer side,
so it must be called from the same thread(s) that call poll.
Fetches multiple elements from the queue, if available.
Fetches multiple elements from the queue, if available.
This operation back-pressures until the minLength
requirement is
achieved.
specifies the minimum length of the returned sequence; the operation back-pressures until this length is satisfied
is the capacity of the used buffer, being the max length of the returned sequence
a future with a sequence of length between minLength and maxLength; it can also be cancelled, interrupting the wait
Pushes a value in the queue, or if the queue is full, then repeats the operation until it succeeds.
Pushes a value in the queue, or if the queue is full, then repeats the operation until it succeeds.
a task that when evaluated, will complete with a value, or wait until such a value is ready
Pushes multiple values in the queue.
Pushes multiple values in the queue. Back-pressures if the queue is full.
a task that will eventually complete when the push has succeeded; it can also be cancelled, interrupting the waiting
Fetches a value from the queue, or if the queue is empty continuously polls the queue until a value is made available.
Fetches a value from the queue, or if the queue is empty continuously polls the queue until a value is made available.
a task that when evaluated, will eventually complete after the value has been successfully pushed in the queue
Try pushing a value to the queue.
Try pushing a value to the queue.
The protocol is unsafe because usage of the "try*" methods imply an understanding of concurrency, or otherwise the code can be very fragile and buggy.
is the value pushed in the queue
true
if the operation succeeded, or false
if the queue is
full and cannot accept any more elements
Try pulling a value out of the queue.
Try pulling a value out of the queue.
The protocol is unsafe because usage of the "try*" methods imply an understanding of concurrency, or otherwise the code can be very fragile and buggy.
Some(a)
in case a value was successfully retrieved from the
queue, or None
in case the queue is empty
A high-performance, back-pressured, generic concurrent queue implementation.
This is the pure and generic version of monix.execution.AsyncQueue.
Example
Back-Pressuring and the Polling Model
The initialized queue can be limited to a maximum buffer size, a size that could be rounded to a power of 2, so you can't rely on it to be precise. Such a bounded queue can be initialized via ConcurrentQueue.bounded. Also see BufferCapacity, the configuration parameter that can be passed in the ConcurrentQueue.custom builder.
On offer, when the queue is full, the implementation back-pressures until the queue has room again in its internal buffer, the future being completed when the value was pushed successfully. Similarly poll awaits the queue to have items in it. This works for both bounded and unbounded queues.
For both
offer
andpoll
, in case awaiting a result happens, the implementation does so asynchronously, without any threads being blocked.Currently the implementation is optimized for speed. In a producer-consumer pipeline the best performance is achieved if the producer(s) and the consumer(s) do not contend for the same resources. This is why when doing asynchronous waiting for the queue to be empty or full, the implementation does so by repeatedly retrying the operation, with asynchronous boundaries and delays, until it succeeds. Fairness is ensured by the implementation.
Multi-threading Scenario
This queue support a ChannelType configuration, for fine tuning depending on the needed multi-threading scenario. And this can yield better performance:
The default is
MPMC
, because that's the safest scenario.WARNING: default is
MPMC
, however any other scenario implies a relaxation of the internal synchronization between threads.This means that using the wrong scenario can lead to severe concurrency bugs. If you're not sure what multi-threading scenario you have, then just stick with the default
MPMC
.