public class LinkedBlockingMultiQueue<K,E> extends AbstractPollable<E>
This class essentially allows a consumer to efficiently block a single thread on a set of queues, until one becomes available. The special feature is that individual queues can be enabled or disabled. A disabled queue is not considered for polling (in the event that all the queue are disabled, any blocking operation would do so trying to read, as if all the queues were empty). Elements are taken from the set of enabled queues, obeying the established priority (queues with the same priority are served round robin).
A disabled queue accepts new elements normally until it reaches the maximum capacity (if any).
Individual queues can be added, removed, enabled or disabled at any time.
The optional capacity bound constructor argument serves as a way to prevent excessive queue expansion. The capacity, if unspecified, is equal to Int.MaxVaue. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.
Not being actually a linear queue, this class does not implement the Collection or Queue interfaces.
The traditional queue interface is split in the traits: Offerable and Pollable. Sub-queues do however
implement Collection.
LinkedBlockingQueue| Modifier and Type | Class and Description |
|---|---|
class |
LinkedBlockingMultiQueue.SubQueue
Represent a sub-queue inside a multi-queue.
|
static interface |
LinkedBlockingMultiQueue.SubQueueSelection<K,E>
Allows to choose the next subQueue.
|
| Constructor and Description |
|---|
LinkedBlockingMultiQueue()
Constructor.
|
LinkedBlockingMultiQueue(LinkedBlockingMultiQueue.SubQueueSelection<K,E> subQueueSelection)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
LinkedBlockingMultiQueue.SubQueue |
addSubQueue(K key,
int priority)
Add a sub queue if absent
|
LinkedBlockingMultiQueue.SubQueue |
addSubQueue(K key,
int priority,
int capacity)
Add a sub-queue if absent
|
int |
drainTo(Collection<? super E> c)
Removes all available elements from this queue and adds them to the given collection.
|
int |
drainTo(Collection<? super E> c,
int maxElements)
Removes at most the given number of available elements from this queue and adds them to the given collection.
|
int |
getPriorityGroupsCount()
Counts the priority groups currently registered in
LinkedBlockingMultiQueue. |
LinkedBlockingMultiQueue.SubQueue |
getSubQueue(K key)
Gets a sub-queue
|
boolean |
isEmpty()
Returns whether this multi-queue is empty, that is, whether there is any element ready to be taken from the head.
|
E |
peek()
Retrieves, but does not remove, the head of this queue, or returns
null if this queue is empty. |
E |
poll()
Retrieves and removes the head of this queue, or returns
null if this queue is empty. |
E |
poll(long timeout,
TimeUnit unit)
Retrieves and removes the head of this queue, waiting up to the specified wait time if necessary for an element
to become available.
|
LinkedBlockingMultiQueue.SubQueue |
removeSubQueue(K key)
Remove a sub-queue
|
E |
take()
Retrieves and removes the head of this queue, waiting if necessary until an element becomes available.
|
int |
totalSize()
Returns the total size of this multi-queue, that is, the sum of the sizes of all the enabled sub-queues.
|
element, removepublic LinkedBlockingMultiQueue()
DefaultSubQueueSelection will be used.public LinkedBlockingMultiQueue(LinkedBlockingMultiQueue.SubQueueSelection<K,E> subQueueSelection)
subQueueSelection - an implementation of LinkedBlockingMultiQueue.SubQueueSelectionpublic LinkedBlockingMultiQueue.SubQueue addSubQueue(K key, int priority)
key - the key used to identify the queuepriority - the queue priority, a lower number means higher prioritynull if there was no queue for the keypublic LinkedBlockingMultiQueue.SubQueue addSubQueue(K key, int priority, int capacity)
key - the key used to identify the queuepriority - the queue priority, a lower number means higher prioritycapacity - the capacity of the new sub-queuenull if there was no queue for the keypublic LinkedBlockingMultiQueue.SubQueue removeSubQueue(K key)
key - the key f the sub-queue that should be removedpublic LinkedBlockingMultiQueue.SubQueue getSubQueue(K key)
key - the key f the sub-queue that should be returnedpublic E poll(long timeout, TimeUnit unit) throws InterruptedException
Pollabletimeout - how long to wait before giving up, in units of unitunit - a TimeUnit determining how to interpret the timeout parameternull if the specified waiting time elapses before an element is
availableInterruptedException - if interrupted while waitingpublic E take() throws InterruptedException
PollableInterruptedException - if interrupted while waitingpublic E poll()
Pollablenull if this queue is empty.null if this queue is emptypublic E peek()
Pollablenull if this queue is empty.null if this queue is emptypublic int totalSize()
public boolean isEmpty()
public int drainTo(Collection<? super E> c)
Pollablec may result in elements being in neither, either or both collections when the associated
exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the
behavior of this operation is undefined if the specified collection is modified while the operation is in
progress.c - the collection to transfer elements intopublic int drainTo(Collection<? super E> c, int maxElements)
Pollablec may result in elements being in
neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself
result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified
collection is modified while the operation is in progress.c - the collection to transfer elements intomaxElements - the maximum number of elements to transferpublic int getPriorityGroupsCount()
LinkedBlockingMultiQueue. Suitable
for debugging and testing.Copyright © 2022 Internet2. All rights reserved.