Class MPMCBlockingQueue<E>
java.lang.Object
com.conversantmedia.util.concurrent.MPMCBlockingQueue<E>
- All Implemented Interfaces:
ConcurrentQueue<E>, Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>
public final class MPMCBlockingQueue<E>
extends Object
implements Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>, ConcurrentQueue<E>
Alternative implementation for benchmarking purposes
- Author:
- John Cairns <john@2ad.com> Date: 4/20/16
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final com.conversantmedia.util.concurrent.Conditionprotected final com.conversantmedia.util.concurrent.Conditionprotected final int -
Constructor Summary
ConstructorsConstructorDescriptionMPMCBlockingQueue(int capacity) Construct a blocking queue of the given fixed capacity.MPMCBlockingQueue(int capacity, SpinPolicy spinPolicy) Construct a blocking queue with a given fixed capacityMPMCBlockingQueue(int capacity, Collection<? extends E> c) Construct a blocking queue of the given fixed capacity -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleanaddAll(Collection<? extends E> c) intcapacity()voidclear()clear the queue of all elementsfinal booleanbooleancontainsAll(Collection<?> c) intdrainTo(Collection<? super E> c) intdrainTo(Collection<? super E> c, int maxElements) element()final booleanisEmpty()iterator()final booleanAdd element t to the ringbooleanfinal Epeek()return the first element in the queuefinal Epoll()remove the first element from the queue and return itvoidintremove()intreturn all elements in the queue to the provided array, up to the size of the provided array.booleanbooleanremoveAll(Collection<?> c) booleanretainAll(Collection<?> c) final intsize()take()Object[]toArray()<T> T[]toArray(T[] a) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface BlockingQueue
containsMethods inherited from interface Collection
equals, hashCode, isEmpty, parallelStream, removeIf, size, spliterator, stream, toArray
-
Field Details
-
queueNotFullCondition
protected final com.conversantmedia.util.concurrent.Condition queueNotFullCondition -
queueNotEmptyCondition
protected final com.conversantmedia.util.concurrent.Condition queueNotEmptyCondition -
size
protected final int size
-
-
Constructor Details
-
MPMCBlockingQueue
public MPMCBlockingQueue(int capacity) Construct a blocking queue of the given fixed capacity.
Note: actual capacity will be the next power of two larger than capacity.- Parameters:
capacity- maximum capacity of this queue
-
MPMCBlockingQueue
Construct a blocking queue with a given fixed capacity
Note: actual capacity will be the next power of two larger than capacity. Waiting locking may be used in servers that are tuned for it, waiting locking provides a high performance locking implementation which is approximately a factor of 2 improvement in throughput (40M/s for 1-1 thread transfers) However waiting locking is more CPU aggressive and causes servers that may be configured with far too many threads to show very high load averages. This is probably not as detrimental as it is annoying.- Parameters:
capacity- - the queue capacity, suggest using a power of 2spinPolicy- - determine the level of cpu aggressiveness in waiting
-
MPMCBlockingQueue
Construct a blocking queue of the given fixed capacity
Note: actual capacity will be the next power of two larger than capacity.
The values from the collection, c, are appended to the queue in iteration order. If the number of elements in the collection exceeds the actual capacity, then the additional elements overwrite the previous ones until all elements have been written once.- Parameters:
capacity- maximum capacity of this queuec- A collection to use to populate inital values
-
-
Method Details
-
offer
Description copied from interface:ConcurrentQueueAdd element t to the ring- Specified by:
offerin interfaceBlockingQueue<E>- Specified by:
offerin interfaceConcurrentQueue<E>- Specified by:
offerin interfaceQueue<E>- Parameters:
e- - element to offer- Returns:
- boolean - true if the operation succeeded
-
poll
Description copied from interface:ConcurrentQueueremove the first element from the queue and return it -
remove
Description copied from interface:ConcurrentQueuereturn all elements in the queue to the provided array, up to the size of the provided array.- Specified by:
removein interfaceConcurrentQueue<E>- Parameters:
e- - The element array- Returns:
- int - the number of elements added to t
-
remove
-
element
-
put
- Specified by:
putin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
offer
- Specified by:
offerin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
take
- Specified by:
takein interfaceBlockingQueue<E>- Throws:
InterruptedException
-
poll
- Specified by:
pollin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
clear
public void clear()Description copied from interface:ConcurrentQueueclear the queue of all elements- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceConcurrentQueue<E>
-
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacityin interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<E>
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a) - Specified by:
toArrayin interfaceCollection<E>
-
add
- Specified by:
addin interfaceBlockingQueue<E>- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>
-
remove
- Specified by:
removein interfaceBlockingQueue<E>- Specified by:
removein interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
addAll
- Specified by:
addAllin interfaceCollection<E>
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
iterator
-
peek
Description copied from interface:ConcurrentQueuereturn the first element in the queue- Specified by:
peekin interfaceConcurrentQueue<E>- Returns:
- E - The element
-
size
public final int size()- Specified by:
sizein interfaceConcurrentQueue<E>- Returns:
- int - the number of elements in the queue
-
capacity
public int capacity()- Specified by:
capacityin interfaceConcurrentQueue<E>- Returns:
- int - the capacity of the queue
-
isEmpty
public final boolean isEmpty()- Specified by:
isEmptyin interfaceConcurrentQueue<E>- Returns:
- boolean - true if the queue is currently empty
-
contains
- Specified by:
containsin interfaceConcurrentQueue<E>- Parameters:
o- - the object to test- Returns:
- boolean - true if specified object is contained in the queue
-