Package org.jgroups.util
Class ConcurrentLinkedBlockingQueue2<T>
java.lang.Object
java.util.AbstractCollection<T>
java.util.AbstractQueue<T>
java.util.concurrent.ConcurrentLinkedQueue<T>
org.jgroups.util.ConcurrentLinkedBlockingQueue2<T>
- All Implemented Interfaces:
Serializable,Iterable<T>,Collection<T>,BlockingQueue<T>,Queue<T>
public class ConcurrentLinkedBlockingQueue2<T>
extends ConcurrentLinkedQueue<T>
implements BlockingQueue<T>
Attempt at writing a fast transfer queue, which is bounded. The take() method blocks until there is an element, but
the offer() method drops the element and returns if the queue is full (doesn't block).
The design assumes a number of producers but only one consumer. The consumer only blocks when the queue is empty (on the not-empty condition), the producers block when the queue is full (on the not-full condition). The producers increment a count atomically and if the count is greater than the capacity, they block on the not-full condition. The consumer decrements the condition and signals the not-full condition when the count is capacity -1 (from capacity to capacity-1). The producers signal not-empty when the count is 1 (from 0 to 1)
- Since:
- 3.5
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final intprotected final AtomicIntegerprotected final Condition(package private) intprotected final Lockprivate static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidintdrainTo(Collection<? super T> c) intdrainTo(Collection<? super T> c, int maxElements) protected voidbooleanDrops elements if capacity has been reached.booleanpoll()voidintbooleanintsize()take()protected voidMethods inherited from class java.util.concurrent.ConcurrentLinkedQueue
add, addAll, clear, contains, forEach, isEmpty, iterator, peek, removeAll, removeIf, retainAll, spliterator, toArray, toArray, toStringMethods inherited from class java.util.AbstractQueue
element, removeMethods inherited from class java.util.AbstractCollection
containsAllMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.concurrent.BlockingQueue
add, containsMethods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
capacity
protected final int capacity -
count
-
not_empty_lock
-
not_empty
-
not_empty_awaits
int not_empty_awaits
-
-
Constructor Details
-
ConcurrentLinkedBlockingQueue2
public ConcurrentLinkedBlockingQueue2(int capacity)
-
-
Method Details
-
offer
Drops elements if capacity has been reached. That's OK for the ThreadPoolExecutor as dropped messages will get retransmitted- Specified by:
offerin interfaceBlockingQueue<T>- Specified by:
offerin interfaceQueue<T>- Overrides:
offerin classConcurrentLinkedQueue<T>- Parameters:
t-- Returns:
-
take
- Specified by:
takein interfaceBlockingQueue<T>- Throws:
InterruptedException
-
poll
-
poll
- Specified by:
pollin interfaceBlockingQueue<T>- Throws:
InterruptedException
-
remove
- Specified by:
removein interfaceBlockingQueue<T>- Specified by:
removein interfaceCollection<T>- Overrides:
removein classConcurrentLinkedQueue<T>
-
remainingCapacity
public int remainingCapacity()- Specified by:
remainingCapacityin interfaceBlockingQueue<T>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<T>
-
put
- Specified by:
putin interfaceBlockingQueue<T>- Throws:
InterruptedException
-
offer
- Specified by:
offerin interfaceBlockingQueue<T>- Throws:
InterruptedException
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Overrides:
sizein classConcurrentLinkedQueue<T>
-
drainTo
- Specified by:
drainToin interfaceBlockingQueue<T>
-
waitForNotEmpty
- Throws:
InterruptedException
-
decrCount
protected void decrCount() -
incrCount
protected void incrCount()
-