org.jboss.messaging.utils.concurrent
Class LinkedBlockingDeque<E>

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractQueue<E>
          extended by org.jboss.messaging.utils.concurrent.LinkedBlockingDeque<E>
Type Parameters:
E - the type of elements held in this collection
All Implemented Interfaces:
java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.concurrent.BlockingQueue<E>, java.util.Queue<E>, BlockingDeque<E>, Deque<E>

public class LinkedBlockingDeque<E>
extends java.util.AbstractQueue<E>
implements BlockingDeque<E>, java.io.Serializable

An optionally-bounded blocking deque based on linked nodes.

The optional capacity bound constructor argument serves as a way to prevent excessive expansion. The capacity, if unspecified, is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the deque above capacity.

Most operations run in constant time (ignoring time spent blocking). Exceptions include remove, removeFirstOccurrence, removeLastOccurrence, contains , iterator.remove(), and the bulk operations, all of which run in linear time.

This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. This class is a member of the Java Collections Framework.

Since:
1.6
Author:
Doug Lea
See Also:
Serialized Form

Constructor Summary
LinkedBlockingDeque()
          Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE.
LinkedBlockingDeque(java.util.Collection<? extends E> c)
          Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE, initially containing the elements of the given collection, added in traversal order of the collection's iterator.
LinkedBlockingDeque(int capacity)
          Creates a LinkedBlockingDeque with the given (fixed) capacity.
 
Method Summary
 boolean add(E e)
          Inserts the specified element into the queue represented by this deque unless it would violate capacity restrictions.
 void addFirst(E e)
          Inserts the specified element to the front of this deque unless it would violate capacity restrictions.
 void addLast(E e)
          Inserts the specified element to the end of this deque unless it would violate capacity restrictions.
 void clear()
          Atomically removes all of the elements from this deque.
 boolean contains(java.lang.Object o)
           
 int drainTo(java.util.Collection<? super E> c)
           
 int drainTo(java.util.Collection<? super E> c, int maxElements)
           
 E element()
          Retrieves, but does not remove, the head of the queue represented by this deque.
 E getFirst()
          Retrieves, but does not remove, the first element of this deque.
 E getLast()
          Retrieves, but does not remove, the last element of this deque.
 java.util.Iterator<E> iterator()
          Returns an iterator over the elements in this deque in proper sequence.
 boolean offer(E e)
          Inserts the specified element into the queue represented by this deque unless it would violate capacity restrictions.
 boolean offer(E o, long timeout, java.util.concurrent.TimeUnit unit)
          Inserts the specified element as the lest element of this deque, if possible.
 boolean offerFirst(E o)
          Inserts the specified element to the front this deque unless it would violate capacity restrictions.
 boolean offerFirst(E o, long timeout, java.util.concurrent.TimeUnit unit)
          Inserts the specified element as the first element of this deque, waiting if necessary up to the specified wait time for space to become available.
 boolean offerLast(E o)
          Inserts the specified element to the end of this deque unless it would violate capacity restrictions.
 boolean offerLast(E o, long timeout, java.util.concurrent.TimeUnit unit)
          Inserts the specified element as the last element of this deque, waiting if necessary up to the specified wait time for space to become available.
 E peek()
          Retrieves, but does not remove, the head of the queue represented by this deque, returning null if this deque is empty.
 E peekFirst()
          Retrieves, but does not remove, the first element of this deque, returning null if this deque is empty.
 E peekLast()
          Retrieves, but does not remove, the last element of this deque, returning null if this deque is empty.
 E poll()
          Retrieves and removes the head of the queue represented by this deque, or null if this deque is empty.
 E poll(long timeout, java.util.concurrent.TimeUnit unit)
          Retrieves and removes the first element of this deque, waiting if necessary up to the specified wait time if no elements are present on this deque.
 E pollFirst()
          Retrieves and removes the first element of this deque, or null if this deque is empty.
 E pollFirst(long timeout, java.util.concurrent.TimeUnit unit)
          Retrieves and removes the first element of this deque, waiting if necessary up to the specified wait time if no elements are present on this deque.
 E pollLast()
          Retrieves and removes the last element of this deque, or null if this deque is empty.
 E pollLast(long timeout, java.util.concurrent.TimeUnit unit)
          Retrieves and removes the last element of this deque, waiting if necessary up to the specified wait time if no elements are present on this deque.
 E pop()
          Pops an element from the stack represented by this deque.
 void push(E e)
          Pushes an element onto the stack represented by this deque.
 void put(E o)
          Adds the specified element as the last element of this deque, waiting if necessary for space to become available.
 void putFirst(E o)
          Adds the specified element as the first element of this deque, waiting if necessary for space to become available.
 void putLast(E o)
          Adds the specified element as the last element of this deque, waiting if necessary for space to become available.
 int remainingCapacity()
          Returns the number of elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking.
 E remove()
          Retrieves and removes the head of the queue represented by this deque.
 boolean remove(java.lang.Object o)
           
 E removeFirst()
          Removes and returns the first element of this deque.
 boolean removeFirstOccurrence(java.lang.Object e)
          Removes the first occurrence of the specified element in this deque.
 E removeLast()
          Retrieves and removes the last element of this deque.
 boolean removeLastOccurrence(java.lang.Object e)
          Removes the last occurrence of the specified element in this deque.
 int size()
          Returns the number of elements in this deque.
 E take()
          Retrieves and removes the first element of this deque, waiting if no elements are present on this deque.
 E takeFirst()
          Retrieves and removes the first element of this deque, waiting if no elements are present on this deque.
 E takeLast()
          Retrieves and removes the last element of this deque, waiting if no elements are present on this deque.
 java.lang.Object[] toArray()
           
<T> T[]
toArray(T[] a)
           
 java.lang.String toString()
           
 
Methods inherited from class java.util.AbstractQueue
addAll
 
Methods inherited from class java.util.AbstractCollection
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.Collection
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll
 
Methods inherited from interface java.util.Collection
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll
 

Constructor Detail

LinkedBlockingDeque

public LinkedBlockingDeque()
Creates a LinkedBlockingDeque with a capacity of Integer.MAX_VALUE.


LinkedBlockingDeque

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

Parameters:
capacity - the capacity of this deque
Throws:
java.lang.IllegalArgumentException - if capacity is less than 1

LinkedBlockingDeque

public LinkedBlockingDeque(java.util.Collection<? extends E> c)
Creates a LinkedBlockingDeque 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:
java.lang.NullPointerException - if c or any element within it is null
Method Detail

offerFirst

public boolean offerFirst(E o)
Description copied from interface: Deque
Inserts the specified element to the front this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to method addFirst, which can fail to insert an element only by throwing an exception.

Specified by:
offerFirst in interface Deque<E>
Parameters:
o - the element to insert
Returns:
true if it was possible to insert the element, else false

offerLast

public boolean offerLast(E o)
Description copied from interface: Deque
Inserts the specified element to the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, this method is generally preferable to method addLast which can fail to insert an element only by throwing an exception.

Specified by:
offerLast in interface Deque<E>
Parameters:
o - the element to insert
Returns:
true if it was possible to insert the element, else false

addFirst

public void addFirst(E e)
Description copied from interface: Deque
Inserts the specified element to the front of this deque unless it would violate capacity restrictions.

Specified by:
addFirst in interface Deque<E>
Parameters:
e - the element to insert

addLast

public void addLast(E e)
Description copied from interface: Deque
Inserts the specified element to the end of this deque unless it would violate capacity restrictions.

Specified by:
addLast in interface Deque<E>
Parameters:
e - the element to insert

pollFirst

public E pollFirst()
Description copied from interface: Deque
Retrieves and removes the first element of this deque, or null if this deque is empty.

Specified by:
pollFirst in interface Deque<E>
Returns:
the first element of this deque, or null if this deque is empty

pollLast

public E pollLast()
Description copied from interface: Deque
Retrieves and removes the last element of this deque, or null if this deque is empty.

Specified by:
pollLast in interface Deque<E>
Returns:
the last element of this deque, or null if this deque is empty

removeFirst

public E removeFirst()
Description copied from interface: Deque
Removes and returns the first element of this deque. This method differs from the pollFirst method only in that it throws an exception if this deque is empty.

Specified by:
removeFirst in interface Deque<E>
Returns:
the first element of this deque

removeLast

public E removeLast()
Description copied from interface: Deque
Retrieves and removes the last element of this deque. This method differs from the pollLast method only in that it throws an exception if this deque is empty.

Specified by:
removeLast in interface Deque<E>
Returns:
the last element of this deque

peekFirst

public E peekFirst()
Description copied from interface: Deque
Retrieves, but does not remove, the first element of this deque, returning null if this deque is empty.

Specified by:
peekFirst in interface Deque<E>
Returns:
the first element of this deque, or null if this deque is empty

peekLast

public E peekLast()
Description copied from interface: Deque
Retrieves, but does not remove, the last element of this deque, returning null if this deque is empty.

Specified by:
peekLast in interface Deque<E>
Returns:
the last element of this deque, or null if this deque is empty

getFirst

public E getFirst()
Description copied from interface: Deque
Retrieves, but does not remove, the first element of this deque. This method differs from the peek method only in that it throws an exception if this deque is empty.

Specified by:
getFirst in interface Deque<E>
Returns:
the first element of this deque

getLast

public E getLast()
Description copied from interface: Deque
Retrieves, but does not remove, the last element of this deque. This method differs from the peek method only in that it throws an exception if this deque is empty.

Specified by:
getLast in interface Deque<E>
Returns:
the last element of this deque

putFirst

public void putFirst(E o)
              throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Adds the specified element as the first element of this deque, waiting if necessary for space to become available.

Specified by:
putFirst in interface BlockingDeque<E>
Parameters:
o - the element to add
Throws:
java.lang.InterruptedException - if interrupted while waiting.

putLast

public void putLast(E o)
             throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Adds the specified element as the last element of this deque, waiting if necessary for space to become available.

Specified by:
putLast in interface BlockingDeque<E>
Parameters:
o - the element to add
Throws:
java.lang.InterruptedException - if interrupted while waiting.

takeFirst

public E takeFirst()
            throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Retrieves and removes the first element of this deque, waiting if no elements are present on this deque.

Specified by:
takeFirst in interface BlockingDeque<E>
Returns:
the head of this deque
Throws:
java.lang.InterruptedException - if interrupted while waiting.

takeLast

public E takeLast()
           throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Retrieves and removes the last element of this deque, waiting if no elements are present on this deque.

Specified by:
takeLast in interface BlockingDeque<E>
Returns:
the head of this deque
Throws:
java.lang.InterruptedException - if interrupted while waiting.

offerFirst

public boolean offerFirst(E o,
                          long timeout,
                          java.util.concurrent.TimeUnit unit)
                   throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Inserts the specified element as the first element of this deque, waiting if necessary up to the specified wait time for space to become available.

Specified by:
offerFirst in interface BlockingDeque<E>
Parameters:
o - the element to add
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
true if successful, or false if the specified waiting time elapses before space is available.
Throws:
java.lang.InterruptedException - if interrupted while waiting.

offerLast

public boolean offerLast(E o,
                         long timeout,
                         java.util.concurrent.TimeUnit unit)
                  throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Inserts the specified element as the last element of this deque, waiting if necessary up to the specified wait time for space to become available.

Specified by:
offerLast in interface BlockingDeque<E>
Parameters:
o - the element to add
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
true if successful, or false if the specified waiting time elapses before space is available.
Throws:
java.lang.InterruptedException - if interrupted while waiting.

pollFirst

public E pollFirst(long timeout,
                   java.util.concurrent.TimeUnit unit)
            throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Retrieves and removes the first element of this deque, waiting if necessary up to the specified wait time if no elements are present on this deque.

Specified by:
pollFirst in interface BlockingDeque<E>
Parameters:
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the head of this deque, or null if the specified waiting time elapses before an element is present.
Throws:
java.lang.InterruptedException - if interrupted while waiting.

pollLast

public E pollLast(long timeout,
                  java.util.concurrent.TimeUnit unit)
           throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Retrieves and removes the last element of this deque, waiting if necessary up to the specified wait time if no elements are present on this deque.

Specified by:
pollLast in interface BlockingDeque<E>
Parameters:
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the head of this deque, or null if the specified waiting time elapses before an element is present.
Throws:
java.lang.InterruptedException - if interrupted while waiting.

offer

public boolean offer(E e)
Description copied from interface: Deque
Inserts the specified element into the queue represented by this deque unless it would violate capacity restrictions. In other words, inserts the specified element to the end of this deque. When using a capacity-restricted deque, this method is generally preferable to the Deque.add(E) method, which can fail to insert an element only by throwing an exception.

This method is equivalent to Deque.offerLast(E).

Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Specified by:
offer in interface java.util.Queue<E>
Specified by:
offer in interface Deque<E>
Parameters:
e - the element to insert
Returns:
true if it was possible to insert the element, else false

add

public boolean add(E e)
Description copied from interface: Deque
Inserts the specified element into the queue represented by this deque unless it would violate capacity restrictions. In other words, inserts the specified element as the last element of this deque.

This method is equivalent to Deque.addLast(E).

Specified by:
add in interface java.util.Collection<E>
Specified by:
add in interface java.util.concurrent.BlockingQueue<E>
Specified by:
add in interface Deque<E>
Overrides:
add in class java.util.AbstractQueue<E>
Parameters:
e - the element to insert
Returns:
true (as per the spec for Collection.add(E))

push

public void push(E e)
Description copied from interface: Deque
Pushes an element onto the stack represented by this deque. In other words, inserts the element to the front this deque unless it would violate capacity restrictions.

This method is equivalent to Deque.addFirst(E).

Specified by:
push in interface Deque<E>

poll

public E poll()
Description copied from interface: Deque
Retrieves and removes the head of the queue represented by this deque, or null if this deque is empty. In other words, retrieves and removes the first element of this deque, or null if this deque is empty.

This method is equivalent to Deque.pollFirst().

Specified by:
poll in interface java.util.Queue<E>
Specified by:
poll in interface Deque<E>
Returns:
the first element of this deque, or null if this deque is empty

remove

public E remove()
Description copied from interface: Deque
Retrieves and removes the head of the queue represented by this deque. This method differs from the poll method only in that it throws an exception if this deque is empty.

This method is equivalent to Deque.removeFirst().

Specified by:
remove in interface java.util.Queue<E>
Specified by:
remove in interface Deque<E>
Overrides:
remove in class java.util.AbstractQueue<E>
Returns:
the head of the queue represented by this deque

pop

public E pop()
Description copied from interface: Deque
Pops an element from the stack represented by this deque. In other words, removes and returns the the first element of this deque.

This method is equivalent to Deque.removeFirst().

Specified by:
pop in interface Deque<E>
Returns:
the element at the front of this deque (which is the top of the stack represented by this deque)

peek

public E peek()
Description copied from interface: Deque
Retrieves, but does not remove, the head of the queue represented by this deque, returning null if this deque is empty.

This method is equivalent to Deque.peekFirst()

Specified by:
peek in interface java.util.Queue<E>
Specified by:
peek in interface Deque<E>
Returns:
the head of the queue represented by this deque, or null if this deque is empty

element

public E element()
Description copied from interface: Deque
Retrieves, but does not remove, the head of the queue represented by this deque. This method differs from the peek method only in that it throws an exception if this deque is empty.

This method is equivalent to Deque.getFirst()

Specified by:
element in interface java.util.Queue<E>
Specified by:
element in interface Deque<E>
Overrides:
element in class java.util.AbstractQueue<E>
Returns:
the head of the queue represented by this deque

remove

public boolean remove(java.lang.Object o)
Specified by:
remove in interface java.util.Collection<E>
Overrides:
remove in class java.util.AbstractCollection<E>

put

public void put(E o)
         throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Adds the specified element as the last element of this deque, waiting if necessary for space to become available. This method is equivalent to to putLast

Specified by:
put in interface java.util.concurrent.BlockingQueue<E>
Specified by:
put in interface BlockingDeque<E>
Parameters:
o - the element to add
Throws:
java.lang.InterruptedException - if interrupted while waiting.

take

public E take()
       throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Retrieves and removes the first element of this deque, waiting if no elements are present on this deque. This method is equivalent to to takeFirst

Specified by:
take in interface java.util.concurrent.BlockingQueue<E>
Specified by:
take in interface BlockingDeque<E>
Returns:
the head of this deque
Throws:
java.lang.InterruptedException - if interrupted while waiting.

offer

public boolean offer(E o,
                     long timeout,
                     java.util.concurrent.TimeUnit unit)
              throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Inserts the specified element as the lest element of this deque, if possible. When using deques that may impose insertion restrictions (for example capacity bounds), method offer is generally preferable to method Collection.add(E), which can fail to insert an element only by throwing an exception. This method is equivalent to to offerLast

Specified by:
offer in interface java.util.concurrent.BlockingQueue<E>
Specified by:
offer in interface BlockingDeque<E>
Parameters:
o - the element to add.
Returns:
true if it was possible to add the element to this deque, else false
Throws:
java.lang.InterruptedException

poll

public E poll(long timeout,
              java.util.concurrent.TimeUnit unit)
       throws java.lang.InterruptedException
Description copied from interface: BlockingDeque
Retrieves and removes the first element of this deque, waiting if necessary up to the specified wait time if no elements are present on this deque. This method is equivalent to to pollFirst

Specified by:
poll in interface java.util.concurrent.BlockingQueue<E>
Specified by:
poll in interface BlockingDeque<E>
Parameters:
timeout - how long to wait before giving up, in units of unit
unit - a TimeUnit determining how to interpret the timeout parameter
Returns:
the head of this deque, or null if the specified waiting time elapses before an element is present.
Throws:
java.lang.InterruptedException - if interrupted while waiting.

size

public int size()
Returns the number of elements in this deque.

Specified by:
size in interface java.util.Collection<E>
Specified by:
size in class java.util.AbstractCollection<E>
Returns:
the number of elements in this deque.

remainingCapacity

public int remainingCapacity()
Returns the number of elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking. This is always equal to the initial capacity of this deque less the current size of this deque.

Note that you cannot always tell if an attempt to add an element will succeed by inspecting remainingCapacity because it may be the case that a waiting consumer is ready to take an element out of an otherwise full deque.

Specified by:
remainingCapacity in interface java.util.concurrent.BlockingQueue<E>

contains

public boolean contains(java.lang.Object o)
Specified by:
contains in interface java.util.Collection<E>
Overrides:
contains in class java.util.AbstractCollection<E>

removeFirstOccurrence

public boolean removeFirstOccurrence(java.lang.Object e)
Description copied from interface: Deque
Removes the first occurrence of the specified element in this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that (o==null ? e==null : o.equals(e)) (if such an element exists).

Specified by:
removeFirstOccurrence in interface Deque<E>
Parameters:
e - element to be removed from this deque, if present
Returns:
true if the deque contained the specified element

removeLastOccurrence

public boolean removeLastOccurrence(java.lang.Object e)
Description copied from interface: Deque
Removes the last occurrence of the specified element in this deque. If the deque does not contain the element, it is unchanged. More formally, removes the last element e such that (o==null ? e==null : o.equals(e)) (if such an element exists).

Specified by:
removeLastOccurrence in interface Deque<E>
Parameters:
e - element to be removed from this deque, if present
Returns:
true if the deque contained the specified element

toArray

public java.lang.Object[] toArray()
Specified by:
toArray in interface java.util.Collection<E>
Overrides:
toArray in class java.util.AbstractCollection<E>

toArray

public <T> T[] toArray(T[] a)
Specified by:
toArray in interface java.util.Collection<E>
Overrides:
toArray in class java.util.AbstractCollection<E>

toString

public java.lang.String toString()
Overrides:
toString in class java.util.AbstractCollection<E>

clear

public void clear()
Atomically removes all of the elements from this deque. The deque will be empty after this call returns.

Specified by:
clear in interface java.util.Collection<E>
Overrides:
clear in class java.util.AbstractQueue<E>

drainTo

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

drainTo

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

iterator

public java.util.Iterator<E> iterator()
Returns an iterator over the elements in this deque in proper sequence. The returned Iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface java.util.Collection<E>
Specified by:
iterator in interface Deque<E>
Specified by:
iterator in class java.util.AbstractCollection<E>
Returns:
an iterator over the elements in this deque in proper sequence.


Copyright © 2006 JBoss Inc. All Rights Reserved.