edu.emory.mathcs.backport.java.util.concurrent
public class LinkedBlockingDeque extends AbstractQueue implements BlockingDeque, Serializable
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
Constructor Summary | |
---|---|
LinkedBlockingDeque()
Creates a LinkedBlockingDeque with a capacity of
Integer#MAX_VALUE. | |
LinkedBlockingDeque(int capacity)
Creates a LinkedBlockingDeque with the given (fixed) capacity.
| |
LinkedBlockingDeque(Collection 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.
|
Method Summary | |
---|---|
boolean | add(Object e)
Inserts the specified element at the end of this deque unless it would
violate capacity restrictions. |
void | addFirst(Object e) |
void | addLast(Object e) |
void | clear()
Atomically removes all of the elements from this deque.
|
boolean | contains(Object o)
Returns true if this deque contains the specified element.
|
Iterator | descendingIterator()
Returns an iterator over the elements in this deque in reverse
sequential order. |
int | drainTo(Collection c) |
int | drainTo(Collection c, int maxElements) |
Object | element()
Retrieves, but does not remove, the head of the queue represented by
this deque. |
Object | getFirst() |
Object | getLast() |
Iterator | iterator()
Returns an iterator over the elements in this deque in proper sequence.
|
boolean | offer(Object e) |
boolean | offer(Object e, long timeout, TimeUnit unit) |
boolean | offerFirst(Object e) |
boolean | offerFirst(Object e, long timeout, TimeUnit unit) |
boolean | offerLast(Object e) |
boolean | offerLast(Object e, long timeout, TimeUnit unit) |
Object | peek() |
Object | peekFirst() |
Object | peekLast() |
Object | poll() |
Object | poll(long timeout, TimeUnit unit) |
Object | pollFirst() |
Object | pollFirst(long timeout, TimeUnit unit) |
Object | pollLast() |
Object | pollLast(long timeout, TimeUnit unit) |
Object | pop() |
void | push(Object e) |
void | put(Object e) |
void | putFirst(Object e) |
void | putLast(Object e) |
int | remainingCapacity()
Returns the number of additional elements that this deque can ideally
(in the absence of memory or resource constraints) accept without
blocking. |
Object | remove()
Retrieves and removes the head of the queue represented by this deque.
|
boolean | remove(Object o)
Removes the first occurrence of the specified element from this deque.
|
Object | removeFirst() |
boolean | removeFirstOccurrence(Object o) |
Object | removeLast() |
boolean | removeLastOccurrence(Object o) |
int | size()
Returns the number of elements in this deque.
|
Object | take() |
Object | takeFirst() |
Object | takeLast() |
Object[] | toArray()
Returns an array containing all of the elements in this deque, in
proper sequence (from first to last element).
|
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. |
String | toString() |
Parameters: capacity the capacity of this deque
Throws: IllegalArgumentException if capacity is less than 1
Parameters: c the collection of elements to initially contain
Throws: NullPointerException if the specified collection or any of its elements are null
offer
.
This method is equivalent to LinkedBlockingDeque.
Throws: IllegalStateException if the element cannot be added at this time due to capacity restrictions NullPointerException if the specified element is null
Throws: IllegalStateException {@inheritDoc } NullPointerException {@inheritDoc }
Throws: IllegalStateException {@inheritDoc } NullPointerException {@inheritDoc }
Parameters: o object to be checked for containment in this deque
Returns: true if this deque contains the specified element
Throws: UnsupportedOperationException {@inheritDoc } ClassCastException {@inheritDoc } NullPointerException {@inheritDoc } IllegalArgumentException {@inheritDoc }
Throws: UnsupportedOperationException {@inheritDoc } ClassCastException {@inheritDoc } NullPointerException {@inheritDoc } IllegalArgumentException {@inheritDoc }
peek
only in that
it throws an exception if this deque is empty.
This method is equivalent to getFirst
.
Returns: the head of the queue represented by this deque
Throws: NoSuchElementException if this deque is empty
Throws: NoSuchElementException {@inheritDoc }
Throws: NoSuchElementException {@inheritDoc }
Returns: an iterator over the elements in this deque in proper sequence
Throws: NullPointerException if the specified element is null
Throws: NullPointerException {@inheritDoc } InterruptedException {@inheritDoc }
Throws: NullPointerException {@inheritDoc }
Throws: NullPointerException {@inheritDoc } InterruptedException {@inheritDoc }
Throws: NullPointerException {@inheritDoc }
Throws: NullPointerException {@inheritDoc } InterruptedException {@inheritDoc }
Throws: NoSuchElementException {@inheritDoc }
Throws: IllegalStateException {@inheritDoc } NullPointerException {@inheritDoc }
Throws: NullPointerException {@inheritDoc } InterruptedException {@inheritDoc }
Throws: NullPointerException {@inheritDoc } InterruptedException {@inheritDoc }
Throws: NullPointerException {@inheritDoc } InterruptedException {@inheritDoc }
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.
poll
only in that it throws an
exception if this deque is empty.
This method is equivalent to removeFirst
.
Returns: the head of the queue represented by this deque
Throws: NoSuchElementException if this deque is empty
This method is equivalent to
removeFirstOccurrence
.
Parameters: o element to be removed from this deque, if present
Returns: true if this deque changed as a result of the call
Throws: NoSuchElementException {@inheritDoc }
Throws: NoSuchElementException {@inheritDoc }
Returns: the number of elements in this deque
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.
Returns: an array containing all of the elements in 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 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().
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
Throws: ArrayStoreException if the runtime type of the specified array is not a supertype of the runtime type of every element in this deque NullPointerException if the specified array is null