Class MultiPriorityBlockingQueue<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractQueue<E>
-
- org.apache.synapse.commons.executors.MultiPriorityBlockingQueue<E>
-
- Type Parameters:
E
- E should implement the Importance interface.
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,BlockingQueue<E>
,Queue<E>
public class MultiPriorityBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>
This queue implements the BlockingQueue interface. The element should implement the Importance interface.Internally Queue is implemented as a set of multiple queues corresponding to some fixed priorities. When inserting an element, it will be put in to one of these queues depending on its importance.
-
-
Constructor Summary
Constructors Constructor Description MultiPriorityBlockingQueue(List<InternalQueue<E>> queues, boolean isFixedQueues, NextQueueAlgorithm<E> algorithm)
Create a queue with the given queues.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
boolean
contains(Object o)
int
drainTo(Collection<? super E> c)
We always give high priority to highest priority elements.int
drainTo(Collection<? super E> c, int maxElements)
We always give high priority to highest priotiry elements.NextQueueAlgorithm<E>
getNextQueueAlgorithm()
List<InternalQueue<E>>
getQueues()
boolean
isEmpty()
boolean
isFixedSizeQueues()
Iterator<E>
iterator()
boolean
offer(E e)
Add the element if space available in the internal queue corresponding to the priority of the object.boolean
offer(E e, long timeout, TimeUnit unit)
Try to add the element within the given time period.E
peek()
E
poll()
Block indefinitely until a object is available for retrieval.E
poll(long timeout, TimeUnit unit)
Get the element from the top of the queue.void
put(E e)
Put the specified value in to the queue.int
remainingCapacity()
boolean
remove(Object o)
int
size()
E
take()
Get an element.Object[]
toArray()
<T> T[]
toArray(T[] a)
String
toString()
-
Methods inherited from class java.util.AbstractQueue
add, addAll, element, remove
-
Methods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll
-
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
-
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, parallelStream, removeAll, removeIf, retainAll, spliterator, stream, toArray
-
-
-
-
Constructor Detail
-
MultiPriorityBlockingQueue
public MultiPriorityBlockingQueue(List<InternalQueue<E>> queues, boolean isFixedQueues, NextQueueAlgorithm<E> algorithm)
Create a queue with the given queues.This method will create a Queue that accepts objects with only the priorities specified. If a object is submitted with a different priority it will result in an IllegalArgumentException. If the algorithm is null, this queue will use the PRRNextQueueAlgorithm.
- Parameters:
queues
- list of InternalQueue to be usedisFixedQueues
- weather fixed size queues are usedalgorithm
- algorithm for calculating next queue
-
-
Method Detail
-
put
public void put(E e) throws InterruptedException
Put the specified value in to the queue. The put will block until space available in the corresponding internal queue.- Specified by:
put
in interfaceBlockingQueue<E>
- Parameters:
e
- object that implements the Importance interface- Throws:
InterruptedException
-
offer
public boolean offer(E e)
Add the element if space available in the internal queue corresponding to the priority of the object.
-
offer
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException
Try to add the element within the given time period. Wait the specified time for space to be available. This method will put the object in to the internal queue with the corresponding priority. This method blocks only if that internal queue is full.- Specified by:
offer
in interfaceBlockingQueue<E>
- Parameters:
e
- element to be addedtimeout
- time to wait if space not availableunit
- time unit- Returns:
- true if the element is added
- Throws:
InterruptedException
- if the thread is interrupted
-
take
public E take() throws InterruptedException
Get an element. Block until an element is available- Specified by:
take
in interfaceBlockingQueue<E>
- Returns:
- an element
- Throws:
InterruptedException
- if the thread is interrupted
-
poll
public E poll(long timeout, TimeUnit unit) throws InterruptedException
Get the element from the top of the queue. If an element is not available wait the specified timeout.- Specified by:
poll
in interfaceBlockingQueue<E>
- Parameters:
timeout
- waiting time for element to be availableunit
- time unit- Returns:
- an object
- Throws:
InterruptedException
-
drainTo
public int drainTo(Collection<? super E> c)
We always give high priority to highest priority elements. We try to drain all the high priority items first.- Specified by:
drainTo
in interfaceBlockingQueue<E>
- Parameters:
c
- collection to drain the items- Returns:
- number of elements copied
-
drainTo
public int drainTo(Collection<? super E> c, int maxElements)
We always give high priority to highest priotiry elements. We try to drain all the high priority items first.- Specified by:
drainTo
in interfaceBlockingQueue<E>
- Parameters:
c
- collection to drain the itemdmaxElements
- maximum elements to copy- Returns:
- number of elements copied
-
poll
public E poll()
Block indefinitely until a object is available for retrieval.
-
remainingCapacity
public int remainingCapacity()
- Specified by:
remainingCapacity
in interfaceBlockingQueue<E>
-
iterator
public Iterator<E> iterator()
- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in classAbstractCollection<E>
-
size
public int size()
- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceCollection<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
remove
public boolean remove(Object o)
- Specified by:
remove
in interfaceBlockingQueue<E>
- Specified by:
remove
in interfaceCollection<E>
- Overrides:
remove
in classAbstractCollection<E>
-
contains
public boolean contains(Object o)
- Specified by:
contains
in interfaceBlockingQueue<E>
- Specified by:
contains
in interfaceCollection<E>
- Overrides:
contains
in classAbstractCollection<E>
-
toString
public String toString()
- Overrides:
toString
in classAbstractCollection<E>
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection<E>
- Overrides:
clear
in classAbstractQueue<E>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
toArray
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
getQueues
public List<InternalQueue<E>> getQueues()
-
getNextQueueAlgorithm
public NextQueueAlgorithm<E> getNextQueueAlgorithm()
-
isFixedSizeQueues
public boolean isFixedSizeQueues()
-
-