Java Queue Usage Summary

Source: Internet
Author: User

Queue overview

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/8A/BA/wKioL1g6QInzGoE-AABumATAFWk847.png "title=" Sogou _ 2016-11-27_10-09-19.png "alt=" Wkiol1g6qinzgoe-aabumatafwk847.png "/>

, on concurrent queues, the JDK provides 2 sets of implementations, one is a high-performance non-blocking queue represented by Concurrentlinkedqueue, and one is a blocking queue represented by the Blockingqueue interface, regardless of which inherits from the queue. queues that use a blocking algorithm can be implemented with a single lock (Queue and exit with the same lock) or two locks (queued and out-of-band with different locks), and non-blocking implementations can be implemented using the cyclic CAS method , which we will analyze each one.



Concurrentlinkedqueue

A queue for high concurrency scenarios, with a lock-free approach (cas+volatile) for high-concurrency performance, usually concurrentlinkedqueue better than blockingqueue.

It is a link node-based non-boundary path security queue, followed by FIFO principle, the head is the first to join, the tail is recently joined, not allowed to add a null element.

Note that add ()/offer () is a method of adding elements, there is no difference, poll ()/peek () is the way to take out the head element, the difference is that poll removes the element, and peek does not.

It is important to note that because of its non-blocking, unlike other normal collections, the method of getting the size of a queue is not a constant time cost, but an O (N), so we should avoid using the size () method as much as possible, and consider using IsEmpty () instead.

Although the use of the cas+volatile mechanism to avoid the lock, but we have to understand that this is only to ensure the security of a single operation, such as Peek (), but multiple operations if you want to ensure that the use of locking mechanism to achieve the effect of synchronization.



Blockingqueue API

Team:

Offer (E): Returns true immediately if the queue is not full, and returns false--> without blocking if the queue is full

Put (E): If the queue is full, block until the array is dissatisfied or the thread is interrupted--blocking

Offer (e E, long timeout, timeunit unit): Inserts an element at the end of the queue and, if the array is full, enters the wait until the wait time expires


Out team:

Poll (): Non-blocking take data, return immediately

Take (): Block get Data

Poll (long timeout, timeunit unit): poll fetching data with a certain time-out



Arrayblockingqueue

An array-based blocking queue implementation that maintains a fixed-length array inside it to cache data objects in the queue, since there is only one lock object (Reentrantlock) inside the arrayblockingqueue, so the read and write is not decoupled, which means that production consumption cannot be fully parallel. Because the length needs to be defined, it is also called a bounded queue .



Linkedblockingqueue

A linked list-based blocking queue implementation, similar to Arrayblockingqueue, also maintains a data buffer queue (a linked list) within its internal structure.

Linkedblockingqueue more efficient processing of concurrent data, because the internal implementation of the use of 2 locks, that is, the implementation of the queue, the team is locked, that is, read and write separation, so that producers, consumers completely reached the parallel.

No definition of length, also called unbounded queue . Of course, when you do not define the length, you need to be aware of the speed of the producer and the speed of the consumer, because by default the queue length is integer.max_value.



Synchronousqueue

A queue without buffers, producer-produced data is directly accessible and consumed by consumers. It is a lightweight blocking queue, because there is no capacity, in usage, only one thread blocks the fetch element, waits for another thread to put an element into the queue, and then waits for the thread to take away immediately, in fact, is to implement a lightweight single-element exchange between threads .



Priorityblockingqueue

Priority-based blocking queues (priority judgment is determined by the Compator object passed in by the constructor, that is, the object in the incoming queue must implement the comparable interface).

When implementing Priorityblockingqueue, the internal control thread synchronizes the lock with a fair lock, which is also a unbounded queue.

in layman's terms, it is not the FIFO queue, but the priority of who gets out first. So can you think about whether the add/offer will be sorted once every time? Do we need to prioritize all sorts? in fact, you can take a look at the Add/take method, you will learn about the Priorityblockingqueue design idea: at Add, do not sort processing, when taking, the choice of the least priority to get out, This avoids the time ordering at add and saves time when taking, because there is no full ordering, just picking a lower priority element.



Delayqueue

A queue with a delay time in which the element is fetched from the queue only if the specified delay time is reached . The elements in the queue must implement the delayed interface with no size limit. In essence, it is achieved with the help of Priorityblockingqueue, with the delay as the priority . There are many scenarios for delay queuing, such as the removal of data from cache timeouts, Task timeout processing, the closing of idle connections, and so on.




This article is from the "Boundless Mind Infinite" blog, please be sure to keep this source http://zhangfengzhe.blog.51cto.com/8855103/1878323

Java Queue Usage Summary

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.