net.sf.beanlib.util.concurrent
Class UnboundedLinkedBlockingQueue<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by net.sf.beanlib.util.concurrent.UnboundedLinkedBlockingQueue<E>
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, BlockingQueue<E>, Queue<E>

public class UnboundedLinkedBlockingQueue<E>
extends AbstractQueue<E>
implements BlockingQueue<E>, Serializable

See Also:
Serialized Form

Field Summary
private  Condition notEmpty
          Wait queue for waiting takes
private  ConcurrentLinkedQueue<E> q
           
private static long serialVersionUID
           
private  ReentrantLock takeLock
          Lock held by take, poll, etc
 
Constructor Summary
UnboundedLinkedBlockingQueue()
          Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE.
UnboundedLinkedBlockingQueue(Collection<? extends E> c)
          Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.
 
Method Summary
 int drainTo(Collection<? super E> c)
           
 int drainTo(Collection<? super E> c, int maxElements)
           
 Iterator<E> iterator()
           
 boolean offer(E e)
          Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full.
 boolean offer(E e, long timeout, TimeUnit unit)
          Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.
 E peek()
           
 E poll()
           
 E poll(long timeout, TimeUnit unit)
           
 void put(E e)
          Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.
 int remainingCapacity()
           
private  void signalNotEmpty()
          Signals a waiting take.
 int size()
           
 E take()
           
 
Methods inherited from class java.util.AbstractQueue
add, addAll, clear, element, remove
 
Methods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
 
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, clear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

q

private final ConcurrentLinkedQueue<E> q

takeLock

private final ReentrantLock takeLock
Lock held by take, poll, etc


notEmpty

private final Condition notEmpty
Wait queue for waiting takes

Constructor Detail

UnboundedLinkedBlockingQueue

public UnboundedLinkedBlockingQueue()
Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE.


UnboundedLinkedBlockingQueue

public UnboundedLinkedBlockingQueue(Collection<? extends E> c)
Creates a LinkedBlockingQueue with a capacity of Integer.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.

Parameters:
c - the collection of elements to initially contain
Throws:
NullPointerException - if the specified collection or any of its elements are null
Method Detail

signalNotEmpty

private void signalNotEmpty()
Signals a waiting take. Called only from put/offer (which do not otherwise ordinarily lock takeLock.)


put

public void put(E e)
         throws InterruptedException
Inserts the specified element at the tail of this queue, waiting if necessary for space to become available.

Specified by:
put in interface BlockingQueue<E>
Throws:
InterruptedException
NullPointerException

offer

public boolean offer(E e,
                     long timeout,
                     TimeUnit unit)
              throws InterruptedException
Inserts the specified element at the tail of this queue, waiting if necessary up to the specified wait time for space to become available.

Specified by:
offer in interface BlockingQueue<E>
Returns:
true if successful, or false if the specified waiting time elapses before space is available.
Throws:
InterruptedException
NullPointerException

offer

public boolean offer(E e)
Inserts the specified element at the tail of this queue if it is possible to do so immediately without exceeding the queue's capacity, returning true upon success and false if this queue is full. When using a capacity-restricted queue, this method is generally preferable to method add, which can fail to insert an element only by throwing an exception.

Specified by:
offer in interface BlockingQueue<E>
Specified by:
offer in interface Queue<E>
Throws:
NullPointerException - if the specified element is null

take

public E take()
       throws InterruptedException
Specified by:
take in interface BlockingQueue<E>
Throws:
InterruptedException

poll

public E poll(long timeout,
              TimeUnit unit)
       throws InterruptedException
Specified by:
poll in interface BlockingQueue<E>
Throws:
InterruptedException

poll

public E poll()
Specified by:
poll in interface Queue<E>

drainTo

public int drainTo(Collection<? super E> c)
Specified by:
drainTo in interface BlockingQueue<E>

drainTo

public int drainTo(Collection<? super E> c,
                   int maxElements)
Specified by:
drainTo in interface BlockingQueue<E>

iterator

public Iterator<E> iterator()
Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in class AbstractCollection<E>

size

public int size()
Specified by:
size in interface Collection<E>
Specified by:
size in class AbstractCollection<E>

remainingCapacity

public int remainingCapacity()
Specified by:
remainingCapacity in interface BlockingQueue<E>

peek

public E peek()
Specified by:
peek in interface Queue<E>