E
- the type of elements held in this collectionpublic final class MPSCQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>
IdleStrategy
so if there is nothing to take,
the thread can idle and eventually can do the more expensive blocking. The blocking is especially a concern for the putting
thread, because it needs to notify the blocked thread.
This MPSCQueue is based on 2 stacks; so the items are put in a reverse order by the putting thread, and by the taking thread
they are reversed in order again so that the original ordering is restored. Using this approach, if there are multiple items
on the stack, the owning thread can take them all using a single cas. Once this is done, the owning thread can process them
one by one and doesn't need to content with the putting threads; reducing contention.Constructor and Description |
---|
MPSCQueue(IdleStrategy idleStrategy)
Creates a new
MPSCQueue with the provided IdleStrategy . |
MPSCQueue(Thread consumerThread,
IdleStrategy idleStrategy)
Creates a new
MPSCQueue with the provided IdleStrategy and consumer thread. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
.
|
int |
drainTo(Collection<? super E> c) |
int |
drainTo(Collection<? super E> c,
int maxElements) |
boolean |
isEmpty() |
Iterator<E> |
iterator() |
boolean |
offer(E item) |
boolean |
offer(E e,
long timeout,
TimeUnit unit) |
E |
peek() |
E |
poll() |
E |
poll(long timeout,
TimeUnit unit) |
void |
put(E e) |
int |
remainingCapacity() |
void |
setConsumerThread(Thread consumerThread)
Sets the consumer thread.
|
int |
size()
.
|
E |
take() |
add, addAll, element, remove
contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
add, contains, remove
addAll, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray
public MPSCQueue(Thread consumerThread, IdleStrategy idleStrategy)
MPSCQueue
with the provided IdleStrategy
and consumer thread.consumerThread
- the Thread that consumes the items.idleStrategy
- the idleStrategy. If null, this consumer will block if the queue is empty.NullPointerException
- when consumerThread is null.public MPSCQueue(IdleStrategy idleStrategy)
MPSCQueue
with the provided IdleStrategy
.idleStrategy
- the idleStrategy. If null, the consumer will block.public void setConsumerThread(Thread consumerThread)
consumerThread
- the consumer thread.NullPointerException
- when consumerThread null.public void clear()
clear
in interface Collection<E>
clear
in class AbstractQueue<E>
public boolean offer(E item)
public E take() throws InterruptedException
take
in interface BlockingQueue<E>
InterruptedException
public int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in class AbstractCollection<E>
public void put(E e) throws InterruptedException
put
in interface BlockingQueue<E>
InterruptedException
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
offer
in interface BlockingQueue<E>
InterruptedException
public E poll(long timeout, TimeUnit unit) throws InterruptedException
poll
in interface BlockingQueue<E>
InterruptedException
public int remainingCapacity()
remainingCapacity
in interface BlockingQueue<E>
public int drainTo(Collection<? super E> c)
drainTo
in interface BlockingQueue<E>
public int drainTo(Collection<? super E> c, int maxElements)
drainTo
in interface BlockingQueue<E>
Copyright © 2018 Hazelcast, Inc.. All Rights Reserved.