com.fsist.util.concurrent

AsyncQueue

Related Doc: package concurrent

class AsyncQueue[T] extends AtomicReference[Either[Queue[Promise[T]], Queue[T]]] with LazyLogging

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.

T

the queue element type

Linear Supertypes
LazyLogging, AtomicReference[Either[Queue[Promise[T]], Queue[T]]], Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. AsyncQueue
  2. LazyLogging
  3. AtomicReference
  4. Serializable
  5. AnyRef
  6. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new AsyncQueue()

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def accumulateAndGet(arg0: Either[Queue[Promise[T]], Queue[T]], arg1: BinaryOperator[Either[Queue[Promise[T]], Queue[T]]]): Either[Queue[Promise[T]], Queue[T]]

    Definition Classes
    AtomicReference
  5. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  6. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  7. final def compareAndSet(arg0: Either[Queue[Promise[T]], Queue[T]], arg1: Either[Queue[Promise[T]], Queue[T]]): Boolean

    Definition Classes
    AtomicReference
  8. final def dequeue(): Future[T]

    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.

    Annotations
    @tailrec()
  9. final def enqueue(t: T): Unit

    Enqueue an item synchronously.

    Enqueue an item synchronously.

    Annotations
    @tailrec()
  10. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. final def get(): Either[Queue[Promise[T]], Queue[T]]

    Definition Classes
    AtomicReference
  14. final def getAndAccumulate(arg0: Either[Queue[Promise[T]], Queue[T]], arg1: BinaryOperator[Either[Queue[Promise[T]], Queue[T]]]): Either[Queue[Promise[T]], Queue[T]]

    Definition Classes
    AtomicReference
  15. final def getAndSet(arg0: Either[Queue[Promise[T]], Queue[T]]): Either[Queue[Promise[T]], Queue[T]]

    Definition Classes
    AtomicReference
  16. final def getAndUpdate(arg0: UnaryOperator[Either[Queue[Promise[T]], Queue[T]]]): Either[Queue[Promise[T]], Queue[T]]

    Definition Classes
    AtomicReference
  17. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  18. def getEnqueued(): Queue[T]

    Returns all entries currently enqueued without dequeueing them.

  19. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  20. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  21. final def lazySet(arg0: Either[Queue[Promise[T]], Queue[T]]): Unit

    Definition Classes
    AtomicReference
  22. lazy val logger: Logger

    Attributes
    protected
    Definition Classes
    LazyLogging
  23. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  24. final def notify(): Unit

    Definition Classes
    AnyRef
  25. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  26. final def set(arg0: Either[Queue[Promise[T]], Queue[T]]): Unit

    Definition Classes
    AtomicReference
  27. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  28. def toString(): String

    Definition Classes
    AtomicReference → AnyRef → Any
  29. final def tryDequeue(): Option[T]

    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.

    Annotations
    @tailrec()
  30. final def updateAndGet(arg0: UnaryOperator[Either[Queue[Promise[T]], Queue[T]]]): Either[Queue[Promise[T]], Queue[T]]

    Definition Classes
    AtomicReference
  31. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  32. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  33. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  34. final def weakCompareAndSet(arg0: Either[Queue[Promise[T]], Queue[T]], arg1: Either[Queue[Promise[T]], Queue[T]]): Boolean

    Definition Classes
    AtomicReference

Inherited from LazyLogging

Inherited from AtomicReference[Either[Queue[Promise[T]], Queue[T]]]

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped