Java Blocking Queue Blockingqueue

Source: Internet
Author: User

Blocking Blockingqueue interface implementations in Java more often than not in arrayblockingqueue,linkedblockingqueue. They are thread-safe. Arrayblockingqueue is stored as an array, The linkedblockingqueue is stored in the same way as node nodes.

In development if the queue's insert operation is frequently recommended to use linkedblockingqueue, since each node has a front and back pointer, inserting a new element requires only a pointer reference before and after the change, Arrayblockingqueue inserts a new element, The subscript position of the element array after the new element is changed and the performance is poor. If the queue's read operations are frequently recommended for use with Arrayblockingqueue, Arrayblockingqueue can find the corresponding element directly through an array subscript, Linkedblockingqueue will need to traverse the node chain to find the element.

Blockingqueue queues commonly used methods of operation:

1. Add elements to the queue: Add (), put (), offer ()

2. Remove or remove elements from the queue: remove () Element () Peek () pool () take ()

Adding new elements to a queue usually adds elements to the end of the team.

The offer () method adds an element to the queue if the queue is full directly returns false, the queue is not full and is inserted directly and returns true;

The Add () method is a simple encapsulation of the offer () method. If the queue is full, throw an exception to the new IllegalStateException ("queue");

The put () method inserts an element into the queue and, if the queue is full, waits until the queue is empty to insert a new element, or the thread is interrupted to throw an exception.

The elements that are removed or removed from the queue are for the team header

The Remove () method deletes the elements of the team header directly:

The peek () method takes the elements of the team header directly and does not delete them.

The element () method is a simple encapsulation of the Peek method, and if the team header element exists then the fetch is not deleted if there is no throw exception nosuchelementexception ()

The pool () method takes out and deletes the elements of the team header, and returns NULL when the queue is empty;

The Take () method takes out and deletes the elements of the team header and, when the queue is empty, waits until the queue has a new element to take out, or the thread is interrupted to throw an exception


The offer () method is generally corresponding to the pool () method, and the put () approach is generally corresponding to take (). The offer () is relatively frequent with the pool () method during the daily development process.

Java Blocking Queue Blockingqueue

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.