OneArrayblockingqueue
First look at a section of source code:
1 Public classArrayblockingqueue<e>extendsAbstractqueue<e>ImplementsBlockingqueue<e>, java.io.Serializable {2 Private Static Final LongSerialversionuid = -817911632652898426l;3 Finalobject[] items;4 intTakeindex;5 intPutindex;6 intcount;7 FinalReentrantlock lock;8 Private FinalCondition Notempty;9 Private FinalCondition notfull;Ten transientItrs Itrs =NULL;
Arrayblockingqueue is an array queue, which is maintained by the code with a object[] items array, and then synchronized to ensure security;
Understanding Arrayblockingqueue Main Understanding two points can be: FIFO principle and synchronous secure access.
①, since the queue is implemented using arrays, how does he guarantee the FIFO principle of the queue? There are three main sequence controls:
Takeindex (items index for next takes, poll, peek, or remove), which is the control sequence when an element is fetched from the queue;
Putindex (items index for next put, offer, or add), which is the control sequence of adding elements to the queue;
Count (number ofelementsin the queue), which is the sequence of elements in the queues;
Takeindex Each element is removed Takeindex +1 (Peek method is special, the element is not removed), and when the value of Tokeindex+1 equals the array items length, Takeindex 0.
Putindex each additional element +1, when the value of putindex+1 equals the length of the array items,putindex 0.
Count increments each element +1,count each element-1.
Therefore, the array based on the three variables of the loop control, the implementation of the queue FIFO. = =,
②, concurrency security is based on Reentrantlock and two condition implementations.
Take a look at the construction method:
1 PublicArrayblockingqueue (intCapacity,BooleanFair) {2 if(Capacity <= 0)3 Throw Newillegalargumentexception ();4 This. Items =Newobject[capacity];5Lock =NewReentrantlock (fair);6Notempty =lock.newcondition ();7Notfull =lock.newcondition ();8}
When using a single-parameter construct, the Fair parameter defaults to False, which is an unfair lock. Synchronization is implemented based on Reentrantlock and two Condition blocking and wakeup,notempty blocking threads when no elements are available in the queue, notfull blocking threads when the full queue is not pluggable.
Second, Linkedblockingqueue
Or first look at the source code:
1 Public classLinkedblockingqueue<e>extendsAbstractqueue<e>2 ImplementsBlockingqueue<e>, java.io.Serializable {3 Private Static Final LongSerialversionuid = -6903933977591709194l;4 5 Static classNode<e> {6 E Item;7Node<e>Next;8 9Node (E x) {item =x;}Ten } One A Private Final intcapacity; - - Private FinalAtomicinteger count =NewAtomicinteger (); the - transientNode<e>head; - - Private transientNode<e>Last ; + - Private FinalReentrantlock Takelock =NewReentrantlock (); + A Private FinalCondition Notempty =takelock.newcondition (); at - Private FinalReentrantlock Putlock =NewReentrantlock (); - - Private FinalCondition notfull = Putlock.newcondition ();
Linkedblockingqueue and Arrayblockingqueue have many similarities, the main differences are as follows:
①linkedblockingqueue uses a one-way list, and Arrayblockingqueue uses an array, so Linkedblockingqueue uses the head node and last node to maintain the FIFO principle.
②linkedblockingqueue uses the Takelock and putlock two locks respectively for new and removed elements, which also causes the element counter Count property to be declared as Atomicinteger atomic operation.
③linkedblockingqueue By default you can specify no queue size, using integer. max_value Default initialization of the queue size.
Third, Priorityblockingqueue queue with priority
See the Properties section of the source code:
Public classPriorityblockingqueue<e>extendsAbstractqueue<e>ImplementsBlockingqueue<e>, java.io.Serializable {Private Static Final LongSerialversionuid = 5595510919245408276L; Private Static Final intDefault_initial_capacity = 11; Private Static Final intMax_array_size = integer.max_value-8; Private transientobject[] queue; Private transient intsize; Private transientcomparator<?SuperE>Comparator; Private FinalReentrantlock Lock; Private FinalCondition Notempty; Private transient volatile intAllocationspinlock; PrivatePriorityqueue<e> Q;
It is still a number of groups, and arrayblockingqueue very much like, but also through a single lock lock, its characteristics are as follows:
①, initial size 11, but will automatically expand, the maximum can be to integer.max_value-8;
②, its queue element must implement the comparator interface so that it is based on the minimum heap and maximum heap ordering of the complete binary tree;
③, Priorityblockingqueue itself does not support serialization, the array is transient decorated before, its serialization will be converted to Priorityqueue, The deserialization is then converted into Priorityblockingqueue itself.
Java Common Queue Analysis