In layman Java Concurrency (19): Concurrent container Part 4 concurrent queue and queue introduction [go]

Source: Internet
Author: User

The queue is a new collection class introduced after JDK 5, which belongs to the members of the Java Collections Framework and is the same level of interface in the collection collection as the List/set. The queue is usually described as a FIFO, not all of which are, for example, priorityqueue in order of precedence (or natural order, with comparator interface).

Describes the entire family system of queue in the Java collections framework.

For the queue, the Offer/remove/poll/element/peek method is added based on the collection, and the Add method is redefined. For these six methods, there are different definitions.

 

throws an exception

return special value

Action Description

Plug In

Add (e)

Offer (e)

Add elements to the tail of the queue

Remove

Remove ()

Poll ()

Remove elements from the head of the queue

Check

Element ()

Peek ()

Returns the element of the queue header without removing this element

Specifically, the specification is not thread-safe for queue, and in order to solve this problem, a blocking queue blockingqueue is introduced. For Blockingqueue, all operations are thread-safe, and the operation of the queue can be blocked until a certain condition is met. Another sub-interface of the queue deque describes a two-way queue. Unlike the queue, Deque allows elements to be added to the head of the queue and deleted at the tail of the queue. This means that deque is a two-way queue. Both functions have a queue that is Blockingdeque, which allows elements to be manipulated at the head and tail of the queue, which should be the most powerful implementation in queue.

LinkedList already existed before JDK 5, and its implementation was a two-way queue. So after JDK 5 will be linkedlist to implement the Deque interface at the same time, so that LinkedList is part of the queue.

Typically, a queue is implemented by a linked list structure, but a linked list means that there is some reference overhead, which is even more expensive if it is a doubly linked list. So in order to save memory, one way is to use a fixed-size array to implement the queue. In this case, the size of the queue is fixed, and the traversal of the element is carried out by the index of the array, which is obviously a model of a doubly linked list. Arraydeque is such an implementation.

In addition, Arrayblockingqueue is also an array implementation of the queue, but did not change the two-way, only the implementation of the Blockingqueue model. Theoretically and arraydeque should be easily transformed into two-way implementations.

Priorityqueue and Priorityblockingqueue implement a sort of queuing model. This is similar to SortedSet, which sorts elements through the comparator interface of the queue or the comparable element. In this case, the entry and exit of the element in the queue is not in the form of a FIFO, but rather according to the natural order of comparison.

Cocurrentlinkedqueue is a thread-safe, non-blocking FIFO queue that is generally simple to implement but effective. It is described in detail in the following chapters.

Synchronousqueue is a special blockingqueue that simply transfers an element of add/offer operation directly to the Remove/take operation. In other words, it does not cache any elements in itself, so it is not a real queue in the strict sense. This queue maintains a list of threads that wait for elements to be added or removed from the queue. Simply put, there is at least one remove/take operation when the Add/offer operation succeeds, and at least one add/offer operation is remove/take operation to succeed. This is a two-way waiting queue model, out of queue waiting to join columns, and queued to wait for the queue. The advantage of such a queue is that it is thread-safe to keep throughput to the maximum thread. So for a task queue that needs to be processed quickly, Synchronousqueue is a good choice.

Blockingqueue also has an implementation of Delayqueue, which allows each element (Delayed) with a delay time, when the call to Take/poll will detect whether the queue header element this time <=0, if satisfied means that has timed out, Then this element can be removed, otherwise it will wait. Specifically, this header element should be the first element to be timed out (this time is the absolute time). This class is cleverly designed to be used in scheduledfuturetask for timed operations. Hopefully, there will be a chapter to talk about this idea. Not really. The thread pool section will definitely mention this.

In layman Java Concurrency (19): Concurrent container Part 4 concurrent queue and queue introduction [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.