This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


java.util.concurrent
class LinkedBlockingDeque

java.lang.Object extended by java.util.AbstractCollection extended by java.util.AbstractQueue extended by java.util.concurrent.LinkedBlockingDeque
All Implemented Interfaces:
Serializable, Collection, Queue, BlockingDeque

Most common way to construct:

LinkedBlockingDeque q = new LinkedBlockingDeque(2);

Based on 146 examples


public class LinkedBlockingDeque
extends AbstractQueue
implements BlockingDeque, Serializable

An optionally-bounded {@linkplain BlockingDeque 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 {@link 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 {@link #remove(Object) remove}, {@link #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence removeLastOccurrence}, {@link #contains contains}, {@link #iterator 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 {@link Collection} and {@link Iterator} interfaces.

This class is a member of the Java Collections Framework.


Constructor Summary

          Creates a LinkedBlockingDeque with a capacity of java.lang.Integer.MAX_VALUE.

          Creates a LinkedBlockingDeque with a capacity of java.lang.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

          Inserts the specified element at the end of this deque unless it would violate capacity restrictions.
 void

          
 void

          
 void

          Atomically removes all of the elements from this deque.
 boolean

          Returns true if this deque contains the specified element.
 Iterator

          Returns an iterator over the elements in this deque in reverse sequential order.
 int

          
 int
drainTo(Collection c, int maxElements)

          
 Object

          Retrieves, but does not remove, the head of the queue represented by this deque.
 Object

          
 Object

          
 Iterator

          Returns an iterator over the elements in this deque in proper sequence.
 boolean

          
 boolean
offer(Object e, long timeout, TimeUnit unit)

          
 boolean

          
 boolean
offerFirst(Object e, long timeout, TimeUnit unit)

          
 boolean

          
 boolean
offerLast(Object e, long timeout, TimeUnit unit)

          
 Object

          
 Object

          
 Object

          
 Object

          
 Object
poll(long timeout, TimeUnit unit)

          
 Object

          
 Object
pollFirst(long timeout, TimeUnit unit)

          
 Object

          
 Object
pollLast(long timeout, TimeUnit unit)

          
 Object
pop()

          
 void

          
 void

          
 void

          
 void

          
 int

          Returns the number of additional elements that this deque can ideally (in the absence of memory or resource constraints) accept without blocking.
 Object

          Retrieves and removes the head of the queue represented by this deque.
 boolean

          Removes the first occurrence of the specified element from this deque.
 Object

          
 boolean

          
 Object

          
 boolean

          
 int

          Returns the number of elements in this deque.
 Object

          
 Object

          
 Object

          
 Object[]

          Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).
 Object[]

          Returns an array containing all of the elements in this deque, in proper sequence; the runtime type of the returned array is that of the specified array.
 String

          Returns a string representation of this collection.
 
Methods inherited from class java.util.AbstractQueue
add, addAll, clear, element, remove
 
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LinkedBlockingDeque

public LinkedBlockingDeque()
Creates a LinkedBlockingDeque with a capacity of {@link Integer#MAX_VALUE}.


LinkedBlockingDeque

public LinkedBlockingDeque(Collection c)
Creates a LinkedBlockingDeque with a capacity of {@link 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

LinkedBlockingDeque

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

Parameters:
capacity - the capacity of this deque
Method Detail

add

public boolean add(Object e)
Inserts the specified element at the end of this deque unless it would violate capacity restrictions. When using a capacity-restricted deque, it is generally preferable to use method {@link #offer(Object) offer}.

This method is equivalent to {@link #addLast}.

Overrides:
add in class AbstractQueue
Parameters:
e

addFirst

public void addFirst(Object e)
Parameters:
e

addLast

public void addLast(Object e)
Parameters:
e

clear

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

Overrides:
clear in class AbstractQueue

contains

public boolean contains(Object o)
Returns true if this deque contains the specified element. More formally, returns true if and only if this deque contains at least one element e such that o.equals(e).

Overrides:
contains in class AbstractCollection
Parameters:
o - object to be checked for containment in this deque
Returns:
true if this deque contains the specified element

descendingIterator

public Iterator descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order. The elements will be returned in order from last (tail) to first (head). The returned Iterator is a "weakly consistent" iterator that will never throw {@link 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.


drainTo

public int drainTo(Collection c)
Parameters:
c

drainTo

public int drainTo(Collection c,
                   int maxElements)
Parameters:
c
maxElements

element

public Object element()
Retrieves, but does not remove, the head of the queue represented by this deque. This method differs from {@link #peek peek} only in that it throws an exception if this deque is empty.

This method is equivalent to {@link #getFirst() getFirst}.

Overrides:
element in class AbstractQueue
Returns:
the head of the queue represented by this deque

getFirst

public Object getFirst()

getLast

public Object getLast()

iterator

public Iterator iterator()
Returns an iterator over the elements in this deque in proper sequence. The elements will be returned in order from first (head) to last (tail). The returned Iterator is a "weakly consistent" iterator that will never throw {@link 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.

Overrides:
iterator in class AbstractCollection
Returns:
an iterator over the elements in this deque in proper sequence

offer

public boolean offer(Object e)
Parameters:
e

offer

public boolean offer(Object e,
                     long timeout,
                     TimeUnit unit)
              throws InterruptedException
Parameters:
e
timeout
unit
Throws:
InterruptedException - {@inheritDoc}

offerFirst

public boolean offerFirst(Object e)
Parameters:
e

offerFirst

public boolean offerFirst(Object e,
                          long timeout,
                          TimeUnit unit)
                   throws InterruptedException
Parameters:
e
timeout
unit
Throws:
InterruptedException - {@inheritDoc}

offerLast

public boolean offerLast(Object e)
Parameters:
e

offerLast

public boolean offerLast(Object e,
                         long timeout,
                         TimeUnit unit)
                  throws InterruptedException
Parameters:
e
timeout
unit
Throws:
InterruptedException - {@inheritDoc}

peek

public Object peek()

peekFirst

public Object peekFirst()

peekLast

public Object peekLast()

poll

public Object poll()

poll

public Object poll(long timeout,
                   TimeUnit unit)
            throws InterruptedException
Parameters:
timeout
unit
Throws:
InterruptedException

pollFirst

public Object pollFirst()

pollFirst

public Object pollFirst(long timeout,
                        TimeUnit unit)
                 throws InterruptedException
Parameters:
timeout
unit
Throws:
InterruptedException

pollLast

public Object pollLast()

pollLast

public Object pollLast(long timeout,
                       TimeUnit unit)
                throws InterruptedException
Parameters:
timeout
unit
Throws:
InterruptedException

pop

public Object pop()

push

public void push(Object e)
Parameters:
e

put

public void put(Object e)
         throws InterruptedException
Parameters:
e
Throws:
InterruptedException - {@inheritDoc}

putFirst

public void putFirst(Object e)
              throws InterruptedException
Parameters:
e
Throws:
InterruptedException - {@inheritDoc}

putLast

public void putLast(Object e)
             throws InterruptedException
Parameters:
e
Throws:
InterruptedException - {@inheritDoc}

remainingCapacity

public int remainingCapacity()
Returns the number of additional 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 insert an element will succeed by inspecting remainingCapacity because it may be the case that another thread is about to insert or remove an element.


remove

public Object remove()
Retrieves and removes the head of the queue represented by this deque. This method differs from {@link #poll poll} only in that it throws an exception if this deque is empty.

This method is equivalent to {@link #removeFirst() removeFirst}.

Overrides:
remove in class AbstractQueue
Returns:
the head of the queue represented by this deque

remove

public boolean remove(Object o)
Removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it is unchanged. More formally, removes the first element e such that o.equals(e) (if such an element exists). Returns true if this deque contained the specified element (or equivalently, if this deque changed as a result of the call).

This method is equivalent to {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.

Overrides:
remove in class AbstractCollection
Parameters:
o - element to be removed from this deque, if present
Returns:
true if this deque changed as a result of the call

removeFirst

public Object removeFirst()

removeFirstOccurrence

public boolean removeFirstOccurrence(Object o)
Parameters:
o

removeLast

public Object removeLast()

removeLastOccurrence

public boolean removeLastOccurrence(Object o)
Parameters:
o

size

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

Overrides:
size in class AbstractCollection
Returns:
the number of elements in this deque

take

public Object take()
            throws InterruptedException
Throws:
InterruptedException

takeFirst

public Object takeFirst()
                 throws InterruptedException
Throws:
InterruptedException

takeLast

public Object takeLast()
                throws InterruptedException
Throws:
InterruptedException

toArray

public Object[] toArray()
Returns an array containing all of the elements in this deque, in proper sequence (from first to last element).

The returned array will be "safe" in that no references to it are maintained by this deque. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Overrides:
toArray in class AbstractCollection
Returns:
an array containing all of the elements in this deque

toArray

public Object[] toArray(Object[] a)
Returns an array containing all of the elements in this deque, in proper sequence; the runtime type of the returned array is that of the specified array. If the deque fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this deque.

If this deque fits in the specified array with room to spare (i.e., the array has more elements than this deque), the element in the array immediately following the end of the deque is set to null.

Like the {@link #toArray()} method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs.

Suppose x is a deque known to contain only strings. The following code can be used to dump the deque into a newly allocated array of String:

     String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().

Overrides:
toArray in class AbstractCollection
Parameters:
a - the array into which the elements of the deque are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose
Returns:
an array containing all of the elements in this deque

toString

public String toString()
Overrides:
toString in class AbstractCollection


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.