Queues in Java

Source: Internet
Author: User

Non-blocking queue:concurrentlinkedqueue

Concurrentlinkedqueue is a link-based, line-free secure queue that uses FIFO rules to sort the nodes, and when we add an element, it is added to the end of the queue, and when we get an element, it returns the element of the queue header.

Blocking queue:blockingqueue

1. Arrayblockingqueue
Array-based blocking queue implementation, within Arrayblockingqueue, maintains a fixed-length array to cache data objects in the queue, which is a common blocking queue, in addition to a fixed-length array, the Arrayblockingqueue interior also holds two shaping variables, Identifies the position of the queue's head and tail in the array, respectively.
Arrayblockingqueue the same lock object is shared between the producer and the consumer, which means that the two cannot actually run in parallel, which is particularly different from the linkedblockingqueue, and according to the principle of implementation, The arrayblockingqueue can be fully split-lock, enabling full parallel operation of both producer and consumer operations. Doug Lea did not do this, perhaps because Arrayblockingqueue's data write and fetch operations are lightweight enough to introduce an independent locking mechanism that, in addition to adding additional complexity to the code, is not a good performance benefit. One notable difference between Arrayblockingqueue and Linkedblockingqueue is that the former does not produce or destroy any additional object instances when inserting or deleting elements, while the latter generates an additional node object. This has a certain difference in the effect of GC on a system that needs to handle large quantities of data efficiently and concurrently over a long period of time. When creating Arrayblockingqueue, we can also control whether an object's internal lock is a fair lock, and an unfair lock is used by default.

2. Linkedblockingqueue
A linked list-based blocking queue, similar to Arraylistblockingqueue, maintains a data buffer queue (which is made up of a list of lists), and when the producer puts a data into the queue, the queue fetches the data from the producer and caches it inside the queue. The producer returns immediately; the producer queue is blocked until the queue buffer reaches the maximum cache capacity (Linkedblockingqueue can be specified by the constructor) until the consumer consumes a piece of data from the queue, and the producer thread is awakened. On the contrary, the consumer side of the processing is based on the same principle. While Linkedblockingqueue is able to efficiently handle concurrency data, it also uses separate locks for both producer and consumer to control data synchronization, which means that producers and consumers can operate the data in the queue in parallel with high concurrency, This improves the concurrency performance of the entire queue.
As a developer, it is important to note that if you construct a Linkedblockingqueue object without specifying its capacity size, Linkedblockingqueue will default to a capacity (Integer.max_value) that is like an infinite size, In this case, if the producer's speed is greater than the consumer's speed, perhaps not until the queue is full of congestion, the system memory may have been exhausted.

3. Delayqueue
The element in the Delayqueue can be fetched from the queue only if its specified delay time is reached. Delayqueue is a queue with no size limit, so the operations (producers) that insert data into the queue are never blocked, and only the operations (consumers) that get the data are blocked.
Usage scenarios:
Delayqueue use fewer scenarios, but they are quite ingenious, and common examples include using a delayqueue to manage a connection queue that is not responding to timeouts.

4. Priorityblockingqueue
Priority-based blocking queues (priority judgments are determined by the Compator objects passed into the constructor), but it is important to note that Priorityblockingqueue does not block data producers, but only the consumers who block the data when there is no consumable data. It is therefore important to pay particular attention to the fact that the producer produces data at a speed that is not faster than the consumer's consumption of data, or that it will eventually deplete all available heap memory space for a long time. When implementing Priorityblockingqueue, the internal control thread synchronizes the lock with a fair lock.

5. Synchronousqueue
A buffer-free waiting queue, similar to a non-intermediary direct transaction, a bit like a primitive society of producers and consumers, producers take products to the market to sell to the final consumer products, and consumers must go to the market to find the direct producers of goods, if one side did not find the right target, then I am sorry, Everyone is waiting in the market. Compared to the buffer blockingqueue, there is a link between the intermediate dealers (buffer), if there is a dealer, the producers directly wholesale the product to the dealer, regardless of the dealer will eventually sell these products to those consumers, because the dealer can stock a part of the goods, Therefore, compared to the direct trading model, the overall use of intermediate dealer mode will be higher throughput (can be sold in bulk); On the other hand, because of the introduction of the dealer, the product from the producer to the consumer to add additional trading links, the performance of a single product can be reduced in time.
There are two different ways to declare a synchronousqueue, and they behave differently. The difference between fair mode and non-equity mode:
If the use of fair mode: Synchronousqueue will be a fair lock, and with a FIFO queue to block redundant producers and consumers, so that the overall system of fairness strategy;
But if the non-fair mode (synchronousqueue default): Synchronousqueue with an unfair lock, with a LIFO queue to manage the surplus producers and consumers, the latter model, if the producers and consumers have a gap in processing speed, is prone to hunger and thirst, where data from certain producers or consumers may never be processed.

Reference:

Http://www.cnblogs.com/jackyuj/archive/2010/11/24/1886553.html

http://ifeve.com/concurrentlinkedqueue/

Queues in Java

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.