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.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classDeprecated.(package private) static classDeprecated.(package private) static final classDeprecated.A proxy that is serialized instead of the queue. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) final AtomicReference<SingleConsumerQueue.Node<E>>[]Deprecated.(package private) static final intDeprecated.The number of slots in the elimination array.(package private) static final intDeprecated.The mask value for indexing into the arena.(package private) final Function<E, SingleConsumerQueue.Node<E>> Deprecated.(package private) static final intDeprecated.The number of CPUs(package private) static final Function<?, ?> Deprecated.The factory for creating an optimistic node.(package private) static final longDeprecated.The offset to the thread-specific probe field.(package private) static final longDeprecated.(package private) static final intDeprecated.The number of times to spin (doing nothing except polling a memory location) before giving up while waiting to eliminate an operation.Fields inherited from class SCQHeader.HeadAndTailRef
tail, TAIL_OFFSETFields inherited from class SCQHeader.PadHeadAndTail
p120, p121, p122, p123, p124, p125, p126, p127, p128, p129, p130, p131, p132, p133, p134, p135, p136, p137, p138, p139, p140, p141, p142, p143, p144, p145, p146, p147, p148, p149, p150, p151, p152, p153, p154, p155, p156, p157, p158, p159, p160, p161, p162, p163, p164, p165, p166, p167, p168, p169, p170, p171, p172, p173, p174, p175, p176, p177, p178, p179, p180, p181, p182, p183, p184, p185, p186, p187, p188, p189, p190, p191, p192, p193, p194, p195, p196, p197, p198, p199, p200, p201, p202, p203, p204, p205, p206, p207, p208, p209, p210, p211, p212, p213, p214, p215, p216, p217, p218, p219, p220, p221, p222, p223, p224, p225, p226, p227, p228, p229, p230, p231, p232, p233, p234, p235, p236, p237, p238, p239Fields inherited from class SCQHeader.HeadRef
headFields inherited from class SCQHeader.PadHead
p000, p001, p002, p003, p004, p005, p006, p007, p008, p009, p010, p011, p012, p013, p014, p015, p016, p017, p018, p019, p020, p021, p022, p023, p024, p025, p026, p027, p028, p029, p030, p031, p032, p033, p034, p035, p036, p037, p038, p039, p040, p041, p042, p043, p044, p045, p046, p047, p048, p049, p050, p051, p052, p053, p054, p055, p056, p057, p058, p059, p060, p061, p062, p063, p064, p065, p066, p067, p068, p069, p070, p071, p072, p073, p074, p075, p076, p077, p078, p079, p080, p081, p082, p083, p084, p085, p086, p087, p088, p089, p090, p091, p092, p093, p094, p095, p096, p097, p098, p099, p100, p101, p102, p103, p104, p105, p106, p107, p108, p109, p110, p111, p112, p113, p114, p115, p116, p117, p118, p119 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSingleConsumerQueue(Function<E, SingleConsumerQueue.Node<E>> factory) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionbooleanDeprecated.booleanaddAll(Collection<? extends E> c) Deprecated.(package private) voidappend(@NonNull SingleConsumerQueue.Node<E> first, @NonNull SingleConsumerQueue.Node<E> last) Deprecated.Adds the linked list of nodes to the queue.(package private) static intceilingPowerOfTwo(int x) Deprecated.booleanDeprecated.(package private) static <E> @NonNull SingleConsumerQueue.Node<E> findLast(@NonNull SingleConsumerQueue.Node<E> node) Deprecated.Returns the last node in the linked list.(package private) static intindex()Deprecated.Returns the arena index for the current thread.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.private voidreadObject(ObjectInputStream stream) Deprecated.intsize()Deprecated.(package private) @Nullable SingleConsumerQueue.Node<E> transferOrCombine(@NonNull SingleConsumerQueue.Node<E> first, SingleConsumerQueue.Node<E> last) Deprecated.Attempts to receive a linked list from a waiting producer or transfer the specified linked list to an arriving producer.(package private) ObjectDeprecated.Methods inherited from class SCQHeader.HeadAndTailRef
casTail, lazySetTailMethods 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
-
Field Details
-
NCPU
static final int NCPUDeprecated.The number of CPUs -
ARENA_LENGTH
static final int ARENA_LENGTHDeprecated.The number of slots in the elimination array. -
ARENA_MASK
static final int ARENA_MASKDeprecated.The mask value for indexing into the arena. -
OPTIMISIC
-
SPINS
static final int SPINSDeprecated.The number of times to spin (doing nothing except polling a memory location) before giving up while waiting to eliminate an operation. Should be zero on uniprocessors. On multiprocessors, this value should be large enough so that two threads exchanging items as fast as possible block only when one of them is stalled (due to GC or preemption), but not much longer, to avoid wasting CPU resources. Seen differently, this value is a little over half the number of cycles of an average context switch time on most systems. The value here is approximately the average of those across a range of tested systems. -
PROBE
static final long PROBEDeprecated.The offset to the thread-specific probe field. -
arena
Deprecated. -
factory
Deprecated. -
serialVersionUID
static final long serialVersionUIDDeprecated.- See Also:
-
-
Constructor Details
-
SingleConsumerQueue
Deprecated.
-
-
Method Details
-
ceilingPowerOfTwo
static int ceilingPowerOfTwo(int x) Deprecated. -
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>
-
append
Deprecated.Adds the linked list of nodes to the queue. -
transferOrCombine
@Nullable SingleConsumerQueue.Node<E> transferOrCombine(@NonNull SingleConsumerQueue.Node<E> first, SingleConsumerQueue.Node<E> last) Deprecated.Attempts to receive a linked list from a waiting producer or transfer the specified linked list to an arriving producer.- Parameters:
first- the first node in the linked list to try to transferlast- the last node in the linked list to try to transfer- Returns:
- either
nullif the element was transferred, the first node if neither a transfer nor receive were successful, or the received last element from a producer
-
index
static int index()Deprecated.Returns the arena index for the current thread. -
findLast
Deprecated.Returns the last node in the linked list. -
iterator
-
writeReplace
Object writeReplace()Deprecated. -
readObject
Deprecated.- Throws:
InvalidObjectException
-