I. Introduction
The blockingqueue interface defines a blocking FIFO queue. Each blockingqueue has a capacity. When the capacity is full, adding data to blockingqueue may cause blocking, when the capacity is empty, the element fetch operation will be blocked. Arrayblockingqueue is an Array Implementation of blockingqueue. It uses a global lock to perform read/write operations on the queue in parallel, two conditional conditions are used to block the fetch operation when the capacity is empty and the write operation when the capacity is full.
II. Specific implementation
The underlying definition of arrayblockingqueue is as follows:
Public class arrayblockingqueue <E> extends abstractqueue <E> implements blockingqueue <E>, Java. io. serializable {// use a circular array to implement queue. Initially, both takeindex and putindex are 0 private final E [] items; private transient int takeindex; private transient int putindex; private int count; // used for concurrent locks and condition private final reentrantlock lock; private final condition notempty; private final condition notfull;/***** loop array * circularly Increment I. */FINAL int Inc (int I) {return (++ I = items. Length )? 0: I;} public arrayblockingqueue (INT capacity, Boolean Fair) {If (capacity <= 0) throw new illegalargumentexception (); this. items = (E []) New object [capacity]; // assign the lock and the condition lock of the lock = new reentrantlock (fair); notempty = lock. newcondition (); notfull = lock. newcondition ();}...}
arrayblockingqueue fetch operation:
Public class arrayblockingqueue <E> extends abstractqueue <E> implements blockingqueue <E>, Java. io. serializable {private e extract () {final E [] items = This. items; e x = items [takeindex]; items [takeindex] = NULL; takeindex = Inc (takeindex); -- count; // activate notfull. signal (); Return X;}/*** the await semantics of condition is as follows: * When a condition-related lock is released in an atomic manner and this thread is disabled * when the method returns, the thread must obtain the lock related to the condition */public e take () throws interruptedexception {final reentrantlock lock = This. lock; lock. lockinterruptibly (); try {// wait for the notempty condition while (COUNT = 0) notempty. await ();} catch (interruptedexception IE) {notempty. signal (); // propagate to non-interrupted thread throw ie;} e x = extract (); Return X;} finally {lock. unlock ();}}...}
write operation of arrayblockingqueue:
Public class arrayblockingqueue <E> extends abstractqueue <E> implements blockingqueue <E>, Java. io. serializable {private void insert (e x) {items [putindex] = x; putindex = Inc (putindex); ++ count; notempty. signal ();} public void put (e o) throws interruptedexception {If (O = NULL) throw new nullpointerexception (); Final E [] items = This. items; Final reentrantlock lock = This. lock; lock. lockinterruptibly (); try {// wait for the notfull condition while (COUNT = items. length) notfull. await ();} catch (interruptedexception IE) {notfull. signal (); // propagate to non-interrupted thread throw ie;} insert (o);} finally {lock. unlock ();}}...}
Note: arrayblockingqueue needs to lock the entire container for read and write operations. Therefore, the throughput is similar to the general implementation and is suitable for implementing the "producer consumer" mode.