Class MultiThreadedQueue<E>
- Type Parameters:
E- Type of element in this queue.
- All Implemented Interfaces:
Iterable<E>,Collection<E>,Queue<E>
- Direct Known Subclasses:
ReentrantAreaLock.Node
ConcurrentLinkedQueue in high-contention reads/writes, and is
not any slower in lower contention reads/writes.
Note that this queue breaks the specification laid out by Collection, see preventAdds() and Collection.add(Object).
This queue will only unlink linked nodes through the peek() and poll() methods, and this is only if
they are at the head of the queue.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classprotected static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected MultiThreadedQueue.LinkedNode<E> protected static final VarHandleprotected MultiThreadedQueue.LinkedNode<E> protected static final VarHandle -
Constructor Summary
ConstructorsConstructorDescriptionConstructs aMultiThreadedQueue, initially empty.MultiThreadedQueue(Iterable<? extends E> collection) Constructs aMultiThreadedQueue, initially containing all elements in the specifiedcollection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanAdds all of the elements from the specified array to this queue.booleanAdds all of the elements from the specified array to this queue.booleanAdds all elements from the specified iterable object to this queue.booleanaddAll(Collection<? extends E> collection) Adds all elements from the specified collection to this queue.booleanaddOrAllowAdds(E element) Atomically adds the specified element to this queue or allows additions to the queue.voidAllows elements to be added to this queue once again.protected final booleanappendList(MultiThreadedQueue.LinkedNode<E> head, MultiThreadedQueue.LinkedNode<E> tail) voidclear()booleanbooleancontainsAll(Collection<?> collection) intEmpties the queue into the specified consumer.intEmpties the queue into the specified consumer.intEmpties the queue into the specified consumer.element()Finds the first element in this queue that matches the predicate.booleanAdds the specified element to the tail of this queue.protected final booleanvoidprotected final MultiThreadedQueue.LinkedNode<E> protected final MultiThreadedQueue.LinkedNode<E> protected final MultiThreadedQueue.LinkedNode<E> protected final MultiThreadedQueue.LinkedNode<E> protected final MultiThreadedQueue.LinkedNode<E> booleanReturns whether this queue is currently add-blocked.booleanisEmpty()iterator()booleanpeek()poll()Retrieves and removes the head of this queue if it matches the specified predicate.Atomically removes the head from this queue if it exists, otherwise prevents additions to this queue if no head is removed.booleanPrevents elements from being added to this queue.remove()booleanbooleanremoveAll(Collection<?> collection) protected final Eprotected final EremoveHead(Predicate<E> predicate) booleanbooleanretainAll(Collection<?> collection) protected final voidsetHeadOpaque(MultiThreadedQueue.LinkedNode<E> newHead) protected final voidsetHeadPlain(MultiThreadedQueue.LinkedNode<E> newHead) protected final voidsetTailOpaque(MultiThreadedQueue.LinkedNode<E> newTail) protected final voidsetTailPlain(MultiThreadedQueue.LinkedNode<E> newTail) intsize()Object[]toArray()<T> T[]toArray(IntFunction<T[]> generator) <T> T[]toArray(T[] array) toString()booleanTries to allow elements to be added to this queue.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
equals, hashCode, parallelStream, stream
-
Field Details
-
head
-
tail
-
HEAD_HANDLE
-
TAIL_HANDLE
-
-
Constructor Details
-
MultiThreadedQueue
public MultiThreadedQueue()Constructs aMultiThreadedQueue, initially empty.The returned object may not be published without synchronization.
-
MultiThreadedQueue
Constructs aMultiThreadedQueue, initially containing all elements in the specifiedcollection.The returned object may not be published without synchronization.
- Parameters:
collection- The specified collection.- Throws:
NullPointerException- Ifcollectionisnullor containsnullelements.
-
-
Method Details
-
setHeadPlain
-
setHeadOpaque
-
getHeadPlain
-
getHeadOpaque
-
getHeadAcquire
-
setTailPlain
-
setTailOpaque
-
getTailPlain
-
getTailOpaque
-
remove
- Specified by:
removein interfaceQueue<E>- Throws:
NoSuchElementException
-
add
Contrary to the specification of
Collection.add(E), this method will fail to add the element to this queue and returnfalseif this queue is add-blocked. -
forceAdd
Adds the specified element to the tail of this queue. If this queue is currently add-locked, then the queue is released from that lock and this element is added. The unlock operation and addition of the specified element is atomic.- Parameters:
element- The specified element.- Returns:
trueif this queue previously allowed additions
-
element
- Specified by:
elementin interfaceQueue<E>- Throws:
NoSuchElementException
-
offer
This method may also return
falseto indicate an element was not added if this queue is add-blocked. -
peek
-
poll
-
pollIf
Retrieves and removes the head of this queue if it matches the specified predicate. If this queue is empty or the head does not match the predicate, this function returnsnull.The predicate may be invoked multiple or no times in this call.
- Parameters:
predicate- The specified predicate.- Returns:
- The head if it matches the predicate, or
nullif it did not or this queue is empty.
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-
preventAdds
public boolean preventAdds()Prevents elements from being added to this queue. Once this is called, any attempt to add to this queue will fail.This function is MT-Safe.
- Returns:
trueif the queue was modified to prevent additions,falseif it already prevented additions.
-
allowAdds
public void allowAdds()Allows elements to be added to this queue once again. Note that this function has undefined behaviour ifpreventAdds()is not called beforehand. The benefit of this function overtryAllowAdds()is that this function might perform better.This function is not MT-Safe.
-
tryAllowAdds
public boolean tryAllowAdds()Tries to allow elements to be added to this queue. Returnstrueif the queue was previous add-locked,falseotherwise.This function is MT-Safe, however it should not be used with
allowAdds().- Returns:
trueif the queue was previously add-locked,falseotherwise.
-
addOrAllowAdds
Atomically adds the specified element to this queue or allows additions to the queue. If additions are not allowed, the element is not added.This function is MT-Safe.
- Parameters:
element- The specified element.- Returns:
trueif the queue now allows additions,falseif the element was added.
-
isAddBlocked
public boolean isAddBlocked()Returns whether this queue is currently add-blocked. That is, whetheradd(Object)and friends will returnfalse. -
pollOrBlockAdds
Atomically removes the head from this queue if it exists, otherwise prevents additions to this queue if no head is removed.This function is MT-Safe.
If the queue is already add-blocked and empty then no operation is performed.- Returns:
nullif the queue is now add-blocked or was previously add-blocked, else returns an non-null value which was the previous head of queue.
-
remove
- Specified by:
removein interfaceCollection<E>
-
removeIf
- Specified by:
removeIfin interfaceCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] array) - Specified by:
toArrayin interfaceCollection<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toString
-
addAll
Adds all elements from the specified collection to this queue. The addition is atomic.- Specified by:
addAllin interfaceCollection<E>- Parameters:
collection- The specified collection.- Returns:
trueif all elements were added successfully, orfalseif this queue is add-blocked, orfalseif the specified collection contains no elements.
-
addAll
Adds all elements from the specified iterable object to this queue. The addition is atomic.- Parameters:
iterable- The specified iterable object.- Returns:
trueif all elements were added successfully, orfalseif this queue is add-blocked, orfalseif the specified iterable contains no elements.
-
addAll
Adds all of the elements from the specified array to this queue.- Parameters:
items- The specified array.- Returns:
trueif all elements were added successfully, orfalseif this queue is add-blocked, orfalseif the specified array has a length of 0.
-
addAll
Adds all of the elements from the specified array to this queue.- Parameters:
items- The specified array.off- The offset in the array.len- The number of items.- Returns:
trueif all elements were added successfully, orfalseif this queue is add-blocked, orfalseif the specified array has a length of 0.
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
iterator
-
size
public int size()Note that this function is computed non-atomically and in O(n) time. The value returned may not be representative of the queue in its current state.
- Specified by:
sizein interfaceCollection<E>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
contains
- Specified by:
containsin interfaceCollection<E>
-
find
Finds the first element in this queue that matches the predicate.- Parameters:
predicate- The predicate to test elements against.- Returns:
- The first element that matched the predicate,
nullif none matched.
-
forEach
-
forceAppendList
protected final boolean forceAppendList(MultiThreadedQueue.LinkedNode<E> head, MultiThreadedQueue.LinkedNode<E> tail) -
appendList
protected final boolean appendList(MultiThreadedQueue.LinkedNode<E> head, MultiThreadedQueue.LinkedNode<E> tail) -
removeHead
-
removeHead
-
drain
Empties the queue into the specified consumer. This function is optimized for single-threaded reads, and should be faster than a loop onpoll().This function is not MT-Safe. This function cannot be called with other read operations (
peek(),poll(),clear(), etc). Write operations are safe to be called concurrently.- Parameters:
consumer- The consumer to accept the elements.- Returns:
- The total number of elements drained.
-
drain
Empties the queue into the specified consumer. This function is optimized for single-threaded reads, and should be faster than a loop onpoll().If
preventAddsistrue, then after this function returns the queue is guaranteed to be empty and additions to the queue will fail.This function is not MT-Safe. This function cannot be called with other read operations (
peek(),poll(),clear(), etc). Write operations are safe to be called concurrently.- Parameters:
consumer- The consumer to accept the elements.preventAdds- Whether to prevent additions to this queue after draining.- Returns:
- The total number of elements drained.
-
drain
Empties the queue into the specified consumer. This function is optimized for single-threaded reads, and should be faster than a loop onpoll().If
preventAddsistrue, then after this function returns the queue is guaranteed to be empty and additions to the queue will fail.This function is not MT-Safe. This function cannot be called with other read operations (
peek(),poll(),clear(),remove(Object)etc). Only write operations are safe to be called concurrently.- Parameters:
consumer- The consumer to accept the elements.preventAdds- Whether to prevent additions to this queue after draining.exceptionHandler- Invoked when the consumer raises an exception.- Returns:
- The total number of elements drained.
-
spliterator
- Specified by:
spliteratorin interfaceCollection<E>- Specified by:
spliteratorin interfaceIterable<E>
-