Today you’ll learn about the last class in the blocking queue, LinkedBlockingDeque.

LinkedBlockingDeque profile

LinkedBlockingDeque is similar to LinkedBlockingQueue, but the underlying LinkedBlockingQueue is a one-way list, while LinkedBlockingDeque is a two-way list. So LinkedBlockingDeque provides more First – and last-ending methods to get or add data from either the head or the tail of the queue, whereas LinkedBlockingQueue only adds data to the tail and gets data from the head.

Basic introduction to

LinkedBlockingDeque inherits AbstractQueue and implements the BlockingDeque interface. BlockingDeque is an extension of BlockingQueue, extending the add, Offer, PUT, take, poll, and other basic methods in BlockingQueue. Methods like addFirst, addLast, offerFirst, offerLast, putFirst, takeLast, etc.

The methods ending in First indicate inserting, getting, or removing the First element of the queue. The last-ending method inserts, retrieves, and retrieves the Last element of the removed queue.

Basic data structure

Internal linked list nodes Node has three main properties:

E item: entry and exit elements. Null indicates that they have been consumed.

Node prev: indicates the Node before the current Node. Null indicates that there is no previous Node.

Node next: indicates the next Node of the current Node. Null indicates that there is no later Node.

This structure is the most basic two-way list structure, one-way list is no prev attribute.

Key attributes are shown below:

As you can see, there is little difference between LinkedBlockingDeque and LinkedBlockingQueue, with the main attributes being the same.

The key way to

By following up the source code of several add, offer and PUT methods, we found that they finally called linkLast method, and these methods ending with Last called linkFirst, so we directly looked at the source code of linkLast and linkFirst methods, the source code is as follows:

The method is relatively simple, a total of two steps:

Get first or last and set the current next or prev so that the node is added to the list;

If first and last are null, the current queue is empty. If last and first are null, the current queue is updated. If not null, the current queue is updated.

The unlinkFirst and unlinkLast methods are called as follows:

The unlink method is also relatively simple, because it removes the head node or tail node from the queue, so you just need to get the corresponding node, and then set its next or prev to first and last, then the node is removed from the list, and finally return the data in the removed node.

Because the lock resource is acquired before the link and unlink methods are called, these two methods are relatively simple.

conclusion

LinkedBlockingDeque is relatively simple as a whole, adding a new method that allows you to insert and remove elements from a queue.

LinkedBlockingQueue can only be first in, first out (FIFO), but LinkedBlockingDeque can implement fifO by providing multiple methods (both consumers and producers call first or last). Special data can also be processed first or last under normal FIFO conditions.

Java programmer daily study notes, such as understanding the wrong welcome to exchange discussion!