org.compass.core.util.concurrent
Class ScalingQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by java.util.concurrent.LinkedBlockingQueue<E>
              extended by org.compass.core.util.concurrent.ScalingQueue<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>

public class ScalingQueue<E>
extends LinkedBlockingQueue<E>

A scaling queue that works with a ThreadPoolExecutor in when offerring which takes the active count and the max threads into account.

Author:
kimchy
See Also:
Serialized Form

Constructor Summary
ScalingQueue()
          Creates a TaskQueue with a capacity of Integer.MAX_VALUE.
ScalingQueue(int capacity)
          Creates a TaskQueue with the given (fixed) capacity.
 
Method Summary
 boolean offer(E o)
          Inserts the specified element at the tail of this queue if there is at least one available thread to run the current task.
 void setThreadPoolExecutor(ThreadPoolExecutor executor)
          Sets the executor this queue belongs to.
 
Methods inherited from class java.util.concurrent.LinkedBlockingQueue
clear, drainTo, drainTo, iterator, offer, peek, poll, poll, put, remainingCapacity, remove, size, take, toArray, toArray, toString
 
Methods inherited from class java.util.AbstractQueue
add, addAll, element, remove
 
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, 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.Queue
element, remove
 
Methods inherited from interface java.util.Collection
addAll, contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll
 

Constructor Detail

ScalingQueue

public ScalingQueue()
Creates a TaskQueue with a capacity of Integer.MAX_VALUE.


ScalingQueue

public ScalingQueue(int capacity)
Creates a TaskQueue with the given (fixed) capacity.

Parameters:
capacity - the capacity of this queue.
Method Detail

setThreadPoolExecutor

public void setThreadPoolExecutor(ThreadPoolExecutor executor)
Sets the executor this queue belongs to.


offer

public boolean offer(E o)
Inserts the specified element at the tail of this queue if there is at least one available thread to run the current task. If all pool threads are actively busy, it rejects the offer.

Specified by:
offer in interface BlockingQueue<E>
Specified by:
offer in interface Queue<E>
Overrides:
offer in class LinkedBlockingQueue<E>
Parameters:
o - the element to add.
Returns:
true if it was possible to add the element to this queue, else false
See Also:
ThreadPoolExecutor.execute(Runnable)


Copyright (c) 2004-2008 The Compass Project.