E
- the type of elements held in this collectionpublic class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
The optional capacity bound constructor argument serves as a
way to prevent excessive queue expansion. The capacity, if unspecified,
is equal to Integer.MAX_VALUE
. Linked nodes are
dynamically created upon each insertion unless this would bring the
queue above capacity.
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.
Constructor and Description |
---|
LinkedBlockingQueue()
Creates a
LinkedBlockingQueue with a capacity of
Integer.MAX_VALUE . |
LinkedBlockingQueue(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. |
LinkedBlockingQueue(int capacity)
Creates a
LinkedBlockingQueue with the given (fixed) capacity. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Atomically removes all of the elements from this queue.
|
boolean |
contains(Object o)
Returns
true if this queue contains the specified element. |
int |
drainTo(Collection<? super E> c)
Removes all available elements from this queue and adds them
to the given collection.
|
int |
drainTo(Collection<? super E> c,
int maxElements)
Removes at most the given number of available elements from
this queue and adds them to the given collection.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this queue in proper sequence.
|
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()
Retrieves, but does not remove, the head of this queue,
or returns null if this queue is empty.
|
E |
poll()
Retrieves and removes the head of this queue,
or returns null if this queue is empty.
|
E |
poll(long timeout,
TimeUnit unit)
Retrieves and removes the head of this queue, waiting up to the
specified wait time if necessary for an element to become available.
|
void |
put(E e)
Inserts the specified element at the tail of this queue, waiting if
necessary for space to become available.
|
int |
remainingCapacity()
Returns the number of additional elements that this queue can ideally
(in the absence of memory or resource constraints) accept without
blocking.
|
boolean |
remove(Object o)
Removes a single instance of the specified element from this queue,
if it is present.
|
int |
size()
Returns the number of elements in this queue.
|
E |
take()
Retrieves and removes the head of this queue, waiting if necessary
until an element becomes available.
|
Object[] |
toArray()
Returns an array containing all of the elements in this queue, in
proper sequence.
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this queue, in
proper sequence; the runtime type of the returned array is that of
the specified array.
|
String |
toString()
Returns a string representation of this collection.
|
add, addAll, element, remove
containsAll, isEmpty, removeAll, retainAll
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
add
addAll, containsAll, equals, hashCode, isEmpty, removeAll, retainAll
public LinkedBlockingQueue()
LinkedBlockingQueue
with a capacity of
Integer.MAX_VALUE
.public LinkedBlockingQueue(int capacity)
LinkedBlockingQueue
with the given (fixed) capacity.capacity
- the capacity of this queueIllegalArgumentException
- if capacity
is not greater
than zeropublic LinkedBlockingQueue(Collection<? extends E> c)
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.c
- the collection of elements to initially containNullPointerException
- if the specified collection or any
of its elements are nullpublic int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public int remainingCapacity()
size
of this queue.
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.
remainingCapacity
in interface BlockingQueue<E>
public void put(E e) throws InterruptedException
put
in interface BlockingQueue<E>
e
- the element to addInterruptedException
- if interrupted while waitingNullPointerException
- if the specified element is null