Ejemplo n.º 1
0
 /**
  * Insert the specified {@link QueueElement} element into the queue.
  *
  * @param queueElement the {@link QueueElement} element to add.
  * @param ignoreSize if the queue is bound to a maximum size and the maximum size is reached, this
  *     parameter (if set to <tt>true</tt>) allows to ignore the maximum size and add the element
  *     to the queue.
  * @return <tt>true</tt> if the element has been inserted, <tt>false</tt> if the element was not
  *     inserted (the queue has reached its maximum size).
  * @throws NullPointerException if the specified element is null
  */
 boolean offer(QueueElement<E> queueElement, boolean ignoreSize) {
   if (queueElement == null) {
     throw new NullPointerException("queueElement is NULL");
   }
   if (queueElement.getPriority() < 0 || queueElement.getPriority() >= priorities) {
     throw new IllegalArgumentException("priority out of range: " + queueElement);
   }
   if (queueElement.inQueue) {
     throw new IllegalStateException("queueElement already in a queue: " + queueElement);
   }
   if (!ignoreSize && currentSize != null && currentSize.get() >= maxSize) {
     return false;
   }
   boolean accepted;
   lock.lock();
   try {
     accepted = queues[queueElement.getPriority()].offer(queueElement);
     debug(
         "offer([{0}]), to P[{1}] delay[{2}ms] accepted[{3}]",
         queueElement.getElement().toString(),
         queueElement.getPriority(),
         queueElement.getDelay(TimeUnit.MILLISECONDS),
         accepted);
     if (accepted) {
       if (currentSize != null) {
         currentSize.incrementAndGet();
       }
       queueElement.inQueue = true;
     }
   } finally {
     lock.unlock();
   }
   return accepted;
 }
Ejemplo n.º 2
0
 /**
  * Retrieve and remove the head of this queue, or return <tt>null</tt> if this queue has no
  * elements with an expired delay.
  *
  * <p>The retrieved element is the oldest one from the highest priority sub-queue.
  *
  * <p>Invocations to this method run the anti-starvation (once every interval check).
  *
  * @return the head of this queue, or <tt>null</tt> if this queue has no elements with an expired
  *     delay.
  */
 @Override
 public QueueElement<E> poll() {
   lock.lock();
   try {
     antiStarvation();
     QueueElement<E> e = null;
     int i = priorities;
     for (; e == null && i > 0; i--) {
       e = queues[i - 1].poll();
     }
     if (e != null) {
       if (currentSize != null) {
         currentSize.decrementAndGet();
       }
       e.inQueue = false;
       debug("poll(): [{0}], from P[{1}]", e.getElement().toString(), i);
     }
     return e;
   } finally {
     lock.unlock();
   }
 }