In layman's Java Concurrency (24): Concurrent container Part 9 bidirectional queue Collection deque[go]

Source: Internet
Author: User

There was no update for some time. Then go on with the last verse.

Queue in addition to the implementation described earlier, there is a two-way queue implementation deque. This queue allows queues to be queued at the head and tail of the queue, so the functionality is more complex than the queue. Describes the complete system diagram of the deque. It is necessary to note that LinkedList has also been part of the deque (LinkedList has a data structure from the beginning of the jdk1.2).

Deque added more methods of operation on the basis of queue.

As can be seen, deque not only has a FIFO queue implementation, there are also filo implementations, that is, not only can implement the queue, can also implement a stack.

As you can see in the Deque architecture diagram, implementing a deque can use an array (arraydeque), you can also use a linked list (LinkedList), and you can also implement a blocking-enabled thread-safe version queue Linkedblockingdeque.

For the deque of the array implementation, the data structure is relatively simple and requires only an array of stored data and an index of up to two indexes. Since the array is fixed-length, it is easy to get the head and tail of the array, then the operation of the array only needs to move the index of the head and tail.

In particular, Arraydeque is not a fixed-size queue that expands the queue capacity by one time (Doublecapacity ()) after each queue is full, so adding an element will always succeed and will not throw an exception. That is, Arraydeque is a queue with no capacity limit.

Similarly, to continue the performance considerations, it is much more efficient to use System.arraycopy to copy an array than to loop the settings.

For LinkedList itself, the data structure is much simpler, except that a size is used to record sizes, only the head element is entry. Comparing the other data structures of map and queue, you can see that the entry here has two references and is a two-way queue.

In, LinkedList always has a "puppet" node that describes the "head" of the queue, but does not represent the head element, which is an empty node that executes null.

The queue starts with a single empty element at the head and then joins E1 (Add/addlast) from the tail, creating a two-way link between head and E1. Then continue to join E2,E2 from the tail to establish a two-way link between head and E1. Finally joins E3 (Push/addfirst) from the head of the queue, so E3 links the two-way link between E1 and head.

The data structure of the doubly linked list is simple and easy to operate, starting from the "puppet" node, the next element of the "puppet" node is the head of the queue, the previous element is the tail of the queue, in other words, the "puppet" node establishes a channel between the head and the tail, the entire queue forms a loop, This makes it possible to traverse the complete queue from either direction in any one of the nodes.

The same linkedlist is also a queue without capacity constraints, so the queue (either from the head or tail) is always successful.

The Arraydeque and LinkedList described above are implemented in two different ways, usually more efficient in traversing and saving memory Arraydeque (indexes are faster and entry objects are not required), but LinkedList is more flexible in queue expansion, Because there is no need to replicate the original queue, it may be more efficient in some cases.

It is also important to note that both implementations are not thread-safe and are therefore only suitable for use in single-threaded environments, and the Linkedblockingdeque described in the following sections are thread-safe and deque. In fact, it should be the most powerful queue implementation, and of course it might be a bit more complicated to implement.

In layman's Java Concurrency (24): Concurrent container Part 9 bidirectional queue Collection deque[go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.