Package org.pipecraft.infra.concurrent
Class LockFreeBlockingQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- org.pipecraft.infra.concurrent.LockFreeBlockingQueue<E>
-
- Type Parameters:
E- The queue item data type
- All Implemented Interfaces:
Iterable<E>,Collection<E>,BlockingQueue<E>,Queue<E>
public class LockFreeBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>
A blocking queue implementation which wraps the efficient ConcurrentLinkedQueue, and adds adaptive yields/sleeps in order to achieve a blocking behavior without using locks. The queue turns out to be more efficient than standard blocking queues (ArrayBlockingQueueandLinkedBlockingQueue), specially in cases of small queue capacity, or when producers and consumers aren't balanced in their rates. This implementation is optimized for overall throughput, but it's not intended for cases where latency of individual put(E) or take() operations is important. Therefore, this implementation is specially suitable for multithreaded pipelines.- Author:
- Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description LockFreeBlockingQueue(int approxSizeLimit)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intdrainTo(Collection<? super E> c)intdrainTo(Collection<? super E> c, int maxElements)Iterator<E>iterator()booleanoffer(E e)booleanoffer(E e, long timeout, TimeUnit unit)Epeek()Epoll()Epoll(long timeout, TimeUnit unit)voidput(E e)intremainingCapacity()intsize()Etake()-
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.concurrent.BlockingQueue
add, contains, remove
-
Methods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
-
-
-
-
Method Detail
-
offer
public boolean offer(E e)
-
offer
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
offerin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
poll
public E poll(long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
pollin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
iterator
public Iterator<E> iterator()
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>
-
size
public int size()
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>
-
put
public void put(E e) throws InterruptedException
- Specified by:
putin interfaceBlockingQueue<E>- Throws:
InterruptedException
-
take
public E take() throws InterruptedException
- Specified by:
takein interfaceBlockingQueue<E>- Throws:
InterruptedException
-
remainingCapacity
public int remainingCapacity()
- Specified by:
remainingCapacityin interfaceBlockingQueue<E>
-
drainTo
public int drainTo(Collection<? super E> c)
- Specified by:
drainToin interfaceBlockingQueue<E>
-
drainTo
public int drainTo(Collection<? super E> c, int maxElements)
- Specified by:
drainToin interfaceBlockingQueue<E>
-
-