Class SingleConsumerQueue<E>
- Type Parameters:
E- the type of elements held in this collection
- All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>
null elements.
A SingleConsumerQueue is an appropriate choice when many producer threads will share
access to a common collection and a single consumer thread drains it. This collection is useful
in scenarios such as implementing flat combining, actors, or lock amortization.
This implementation employs combination to transfer elements between threads that are producing concurrently. This approach avoids contention on the queue by combining colliding operations that have identical semantics. When a pair of producers collide, the task of performing the combined set of operations is delegated to one of the threads and the other thread optionally waits for its operation to be completed. This decision of whether to wait for completion is determined by constructing either a linearizable or optimistic queue.
Iterators are weakly consistent, returning elements reflecting the state of the queue at
some point at or since the creation of the iterator. They do not throw ConcurrentModificationException, and may proceed concurrently with other operations.
Elements contained in the queue since the creation of the iterator will be returned exactly once.
Beware that it is the responsibility of the caller to ensure that a consumer has exclusive read access to the queue. This implementation does not include fail-fast behavior to guard against incorrect consumer usage.
Beware that, unlike in most collections, the size method is NOT a
constant-time operation. Because of the asynchronous nature of these queues, determining the
current number of elements requires a traversal of the elements, and so may report inaccurate
results if this collection is modified during traversal.
Warning: This class is scheduled for removal in version 3.0.0.
- Author:
- ben.manes@gmail.com (Ben Manes)
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbooleanDeprecated.booleanaddAll(Collection<? extends E> c) Deprecated.booleanDeprecated.booleanisEmpty()Deprecated.iterator()Deprecated.static <E> SingleConsumerQueue<E> Deprecated.Creates a queue with a linearizable backoff strategy.booleanDeprecated.static <E> SingleConsumerQueue<E> Deprecated.Creates a queue with an optimistic backoff strategy.peek()Deprecated.poll()Deprecated.intsize()Deprecated.Methods inherited from class AbstractQueue
clear, element, removeMethods inherited from class AbstractCollection
containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
clear, containsAll, equals, hashCode, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Method Details
-
optimistic
Deprecated.Creates a queue with an optimistic backoff strategy. A thread completes its operation without waiting after it successfully hands off the additional element(s) to another producing thread for batch insertion. This optimistic behavior may result in additions not appearing in FIFO order due to the backoff strategy trying to compensate for queue contention.- Type Parameters:
E- the type of elements held in this collection- Returns:
- a new queue where producers complete their operation immediately if combined with another producing thread's
-
linearizable
Deprecated.Creates a queue with a linearizable backoff strategy. A thread waits for a completion signal if it successfully hands off the additional element(s) to another producing thread for batch insertion.- Type Parameters:
E- the type of elements held in this collection- Returns:
- a new queue where producers wait for a completion signal after combining its addition with another producing thread's
-
isEmpty
public boolean isEmpty()Deprecated.- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
size
public int size()Deprecated.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
contains
Deprecated.- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>
-
peek
-
offer
-
poll
-
add
Deprecated.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractQueue<E>
-
addAll
Deprecated.- Specified by:
addAllin interfaceCollection<E>- Overrides:
addAllin classAbstractQueue<E>
-
iterator
-