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 voidclear()booleancontains(Object o)intdrainTo(Collection<? super E> c)We always give high priority to highest priority elements.intdrainTo(Collection<? super E> c, int maxElements)We always give high priority to highest priotiry elements.NextQueueAlgorithm<E>getNextQueueAlgorithm()List<InternalQueue<E>>getQueues()booleanisEmpty()booleanisFixedSizeQueues()Iterator<E>iterator()booleanoffer(E e)Add the element if space available in the internal queue corresponding to the priority of the object.booleanoffer(E e, long timeout, TimeUnit unit)Try to add the element within the given time period.Epeek()Epoll()Block indefinitely until a object is available for retrieval.Epoll(long timeout, TimeUnit unit)Get the element from the top of the queue.voidput(E e)Put the specified value in to the queue.intremainingCapacity()booleanremove(Object o)intsize()Etake()Get an element.Object[]toArray()<T> T[]toArray(T[] a)StringtoString()-
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:
putin 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:
offerin 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:
takein 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:
pollin 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:
drainToin 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:
drainToin 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:
remainingCapacityin interfaceBlockingQueue<E>
-
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>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceCollection<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
remove
public boolean remove(Object o)
- Specified by:
removein interfaceBlockingQueue<E>- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>
-
contains
public boolean contains(Object o)
- Specified by:
containsin interfaceBlockingQueue<E>- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>
-
toString
public String toString()
- Overrides:
toStringin classAbstractCollection<E>
-
clear
public void clear()
- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public Object[] toArray()
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>
-
getQueues
public List<InternalQueue<E>> getQueues()
-
getNextQueueAlgorithm
public NextQueueAlgorithm<E> getNextQueueAlgorithm()
-
isFixedSizeQueues
public boolean isFixedSizeQueues()
-
-