![]() |
VOOZH | about |
LinkedBlockingQueue API is an optionally-bounded queue based on linked nodes. It orders the elements in FIFO(First In First Out) order. The head of this queue is the element that has been there in the queue for the longest time and the tail of the queue is the element that has been in the queue for the shortest time. New elements are inserted at the tail of the queue, and the queue retrieval operations obtain elements at the head of the queue. It belongs to java.util.concurrent package. It extends Object, AbstractCollection and AbstractQueue classes.
The capacity argument serves as a way to prevent excessive queue expansion. The capacity (if not specified) is equal to Integer.MAX_VALUE. Linked nodes are dynamically created upon each insertion unless this would bring the queue above capacity.
LinkedBlockingQueue API is a member of Java Collection Framework.
All implemented interfaces:
1. Serializable 2. Iterable<E> 3. Collection<E> 4. BlockingQueue<E> 5. Queue<E>
Parameters: E — The type of elements in the collection
Syntax:
public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E>, Serializable
Constructors:
Implementation:
Example
The elements of the LinkedBlockingQueue is 1 2 3 The remaining capacity is 2147483644 3 removed true The peak element of the LinkedBlockingQueue is 1 The peak element of the LinkedBlockingQueue is 1 The LinkedBlockingQueue contains 4 :false The LinkedBlockingQueue contains 1 :false The size of the LinkedBlockingQueue is 3 [2, 6, 7]
Methods:
| Method | Type | Description |
|---|---|---|
| clear() | void | Removes all the elements of the LinkedBlockingQueue |
| contains(Object O) | boolean | Returns true if the queue contains the specified element. |
| iterator() | Iterator(<E>) | Returns an iterator over the elements in the queue. |
| offer(E e) | boolean | Inserts the specified element at the tail of this queue if it is possible and returns true upon success otherwise false. |
| offer(E e, long timeout, TimeUnit unit) | boolean | Inserts the specified element at the tail of the queue and wait for the specified time for the space to become available. |
| peek() | E | Retrieves, but does not remove, the head of the queue. |
| poll() | E | Retrieves and removes the head of the queue. |
| put(E e) | void | Inserts the specified element at the tail of this queue, waiting if necessary for space to become available. |
| remainingCapacity() | int | Returns the number of additional elements that this queue can ideally accept without blocking. |
| size() | int | Returns the size of the queue |
| toArray() | Object[] | Converts the queue to an array |
| drainTo(Collection<? super E> c) | int | Removes all available elements from the queue and adds them to the specified collection. |
| take() | E | Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. |
| remove(Object O) | boolean | Removes the specified element from the queue, if it is present. |