The difference between Arrayblockingqueue and Linkedblockingqueue

Source: Internet
Author: User

The implementation of locks in the queue is different

Arrayblockingqueue in the lock is not separated, that is, production and consumption is the same lock;

Linkedblockingqueue in the lock is separate, that is, production is putlock, consumption is Takelock

2. Different operation during production or consumption

Arrayblockingqueue is based on an array, when it is produced and consumed, it is inserted or removed directly from the enumeration object without generating or destroying any additional object instances;

Linkedblockingqueue is based on a linked list, when production and consumption, the need to convert the enumeration object to node<e> for insertion or removal, will generate an additional node object, which in a long period of time requires efficient and concurrent processing of large quantities of data in the system, There are some differences in the effect of GC.

3. The queue size is initialized differently

Arrayblockingqueue is bounded, the size of the queue must be specified;

The Linkedblockingqueue is unbounded and can not specify the size of the queue, but the default is Integer.max_value. Of course, you can also specify the queue size to become bounded.

Attention:

    • When using Linkedblockingqueue, a memory overflow is possible when the default size is used and the production speed is greater than the consumption speed.
    • When using Arrayblockingqueue and Linkedblockingqueue to queue 1 million simple characters respectively,

Linkedblockingqueue consumption is about 10 times times the consumption of Arrayblockingqueue,

That is, Linkedblockingqueue consumes about 1500 milliseconds, while Arrayblockingqueue only takes about 150 milliseconds.

    • By implementing the principle of analysis, Arrayblockingqueue can completely adopt the separation lock, so that the producer and consumer operation of the full parallel operation. 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.

The difference between Arrayblockingqueue and Linkedblockingqueue

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.