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