How to use LinkedBlockingDeque in Java?
LinkedBlockingDeque is a FIFO (First-In-First-Out) double-ended queue in Java, which is implemented using a linked list.
Here are some common methods and uses of LinkedBlockingDeque:
- Instantiate a LinkedBlockingDeque object:
- Create a LinkedBlockingDeque object of type String and assign it to a variable called deque.
- Add elements:
- addFirst(E e): add an element to the front of the queue.
- Add an element to the end of the queue.
- Add the element to the head of the queue and return false if the queue is already full.
- Add the element to the end of the queue, returning false if the queue is already full.
- Retrieve and remove elements.
- removeFirst(): Retrieve and remove the head element of the queue.
- removeLast(): retrieve and eliminate the last element in the queue.
- pollFirst(): Retrieve and remove the head element of the queue, returning null if the queue is empty.
- pollLast(): Retrieve and remove the last element of the queue, returning null if the queue is empty.
- Retrieve elements without removing them.
- getFirst(): Retrieve the first element of the queue without removing it.
- Get the last element of the queue without removing it.
- peekFirst(): Retrieve but do not remove the first element of the queue, returning null if the queue is empty.
- peekLast(): Retrieve, but do not remove, the last element of the queue. Return null if the queue is empty.
- Blocking method:
- Add an element to the head of the queue, blocking if the queue is already full.
- Add the element to the end of the queue and wait if the queue is full.
- takeFirst(): Retrieve and remove the head element from the queue, blocking if the queue is empty.
- takeLast() method: retrieves and removes the last element of a queue, blocking if the queue is empty.
Note: LinkedBlockingDeque is thread-safe and can be operated on by multiple threads simultaneously.