Reprint please indicate source: http://www.cnblogs.com/lighten/p/7510799.html
1. Preface
This chapter describes blocking queue Priorityblockingqueue. This is an unbounded and orderly blocking queue, and the collation is consistent with the Priorityqueue described earlier, only adding blocking operations. The same queue does not support inserting null elements, and inserting non-comparable objects is not supported. Its iterators do not guarantee that the queues remain in any particular order, and if you want to traverse sequentially, consider using Arrays.sort (Pq.toarray ()). The class does not guarantee the order of elements of the same priority, and if you want to enforce the order, you need to consider a custom order or comparator using the second comparison property.
2.priorityblockingqueue2.1 Data structure
Default_initial_capacity: Default Queue capacity 11
Max_array_size: Maximum assignable queue capacity integer.max_value-8, minus 8 because some VMS implement some content in the array header
Queue: An array of queued elements. Balanced binary heap implementation, the parent node subscript is n, the left node is 2n+1, the right node is 2n+2. The smallest element is at the front
Size: The number of elements in the current queue
Comparator: A comparer that determines the sequence of elements in a queue
Lock: Locks for all public methods
Notempty: Blocking condition when queue is empty
Allocationspinlock: Spin lock when expanding array allocation resources, CAS needs
Q:priorityqueue is used only when serializing, in order to be compatible with previous versions. is not null only when serializing and deserializing.
2.2 Basic operations
put an element :
The basic consistent difference between the priorityqueue and the implementation is that it locks up and emits a non-empty signal to wake up the blocked fetch thread. The specific operation principle looks: Priorityqueue. What is worth talking about here is the implementation of Trygrow, which is handled with a while loop, and the following is a concrete implementation.
Its first let go of the lock, and then through the CAS set Allocationspinlock to determine which thread has the capacity to expand permissions, if not grabbed the permission will give up the CPU use. In the end, you have to lock up and start the real expansion. The ability to expand is to calculate the size of the allocation array. I'm not sure why it's such a hassle. Release the lock when allocating an array, and speculate that this will be more efficient.
Other operating procedures are similar to those of Priorityqueue and are no longer introduced.
The Java Collection (22) Priorityblockingqueue