Dequeue an item.
Dequeue an item. The returned future will be completed eventually once someone has enqueued an item that can be dequeued.
Successive calls to this method return logically successive futures (i.e. the first one returned will dequeue first), as long as each call completes its synchronous part (i.e. returns an actual future to the caller) before the next call begins.
Enqueue an item synchronously.
Enqueue an item synchronously.
Returns all entries currently enqueued without dequeueing them.
Try to dequeue an item synchronously if the queue is not empty.
Try to dequeue an item synchronously if the queue is not empty. This competes synchronously with other calls to
dequeue
and tryDequeue
, and fairness is not guaranteed, so this method technically may never complete.
An asynchronous concurrent unbounded queue. Enqueueing completes immediately, while dequeueing returns a Future that is completed once an object can be removed from the queue.
Attribution: base implementation (enqueue/dequeue part) copied from https://groups.google.com/forum/#!topic/scala-user/lyoAdNs3E1o Originally by Viktor Klang.
Implementation notes:
An instance holds either a sequence of enqueued values Seq[T], or a sequence of dequeue() promises waiting to be fulfilled.
Operations are synchronized using AtomicReference. Each modification is attempted using compareAndSet, and if the comparison failed (i.e. another thread won at the race of modifying the AtomicReference) we start the dequeue/enqueue method from scratch.
I replaced the Seq (implicit List) with a Queue to improve performance, since the original code was appending to the end of the List.
the queue element type