src/share/classes/java/util/concurrent/ArrayBlockingQueue.java

Print this page




 114      */
 115     final int inc(int i) {
 116         return (++i == items.length) ? 0 : i;
 117     }
 118 
 119     /**
 120      * Circularly decrement i.
 121      */
 122     final int dec(int i) {
 123         return ((i == 0) ? items.length : i) - 1;
 124     }
 125 
 126     @SuppressWarnings("unchecked")
 127     static <E> E cast(Object item) {
 128         return (E) item;
 129     }
 130 
 131     /**
 132      * Returns item at index i.
 133      */

 134     final E itemAt(int i) {
 135         return this.<E>cast(items[i]);
 136     }
 137 
 138     /**
 139      * Throws NullPointerException if argument is null.
 140      *
 141      * @param v the element
 142      */
 143     private static void checkNotNull(Object v) {
 144         if (v == null)
 145             throw new NullPointerException();
 146     }
 147 
 148     /**
 149      * Inserts element at current put position, advances, and signals.
 150      * Call only when holding lock.
 151      */
 152     private void insert(E x) {
 153         items[putIndex] = x;
 154         putIndex = inc(putIndex);
 155         ++count;
 156         notEmpty.signal();
 157     }
 158 
 159     /**
 160      * Extracts element at current take position, advances, and signals.
 161      * Call only when holding lock.
 162      */
 163     private E extract() {
 164         final Object[] items = this.items;
 165         E x = this.<E>cast(items[takeIndex]);
 166         items[takeIndex] = null;
 167         takeIndex = inc(takeIndex);
 168         --count;
 169         notFull.signal();
 170         return x;
 171     }
 172 
 173     /**
 174      * Deletes item at position i.
 175      * Utility for remove and iterator.remove.
 176      * Call only when holding lock.
 177      */
 178     void removeAt(int i) {
 179         final Object[] items = this.items;
 180         // if removing front item, just advance
 181         if (i == takeIndex) {
 182             items[takeIndex] = null;
 183             takeIndex = inc(takeIndex);
 184         } else {
 185             // slide over all others up through putIndex.


 630     }
 631 
 632     /**
 633      * @throws UnsupportedOperationException {@inheritDoc}
 634      * @throws ClassCastException            {@inheritDoc}
 635      * @throws NullPointerException          {@inheritDoc}
 636      * @throws IllegalArgumentException      {@inheritDoc}
 637      */
 638     public int drainTo(Collection<? super E> c) {
 639         checkNotNull(c);
 640         if (c == this)
 641             throw new IllegalArgumentException();
 642         final Object[] items = this.items;
 643         final ReentrantLock lock = this.lock;
 644         lock.lock();
 645         try {
 646             int i = takeIndex;
 647             int n = 0;
 648             int max = count;
 649             while (n < max) {
 650                 c.add(this.<E>cast(items[i]));

 651                 items[i] = null;
 652                 i = inc(i);
 653                 ++n;
 654             }
 655             if (n > 0) {
 656                 count = 0;
 657                 putIndex = 0;
 658                 takeIndex = 0;
 659                 notFull.signalAll();
 660             }
 661             return n;
 662         } finally {
 663             lock.unlock();
 664         }
 665     }
 666 
 667     /**
 668      * @throws UnsupportedOperationException {@inheritDoc}
 669      * @throws ClassCastException            {@inheritDoc}
 670      * @throws NullPointerException          {@inheritDoc}
 671      * @throws IllegalArgumentException      {@inheritDoc}
 672      */
 673     public int drainTo(Collection<? super E> c, int maxElements) {
 674         checkNotNull(c);
 675         if (c == this)
 676             throw new IllegalArgumentException();
 677         if (maxElements <= 0)
 678             return 0;
 679         final Object[] items = this.items;
 680         final ReentrantLock lock = this.lock;
 681         lock.lock();
 682         try {
 683             int i = takeIndex;
 684             int n = 0;
 685             int max = (maxElements < count) ? maxElements : count;
 686             while (n < max) {
 687                 c.add(this.<E>cast(items[i]));

 688                 items[i] = null;
 689                 i = inc(i);
 690                 ++n;
 691             }
 692             if (n > 0) {
 693                 count -= n;
 694                 takeIndex = i;
 695                 notFull.signalAll();
 696             }
 697             return n;
 698         } finally {
 699             lock.unlock();
 700         }
 701     }
 702 
 703     /**
 704      * Returns an iterator over the elements in this queue in proper sequence.
 705      * The elements will be returned in order from first (head) to last (tail).
 706      *
 707      * <p>The returned {@code Iterator} is a "weakly consistent" iterator that




 114      */
 115     final int inc(int i) {
 116         return (++i == items.length) ? 0 : i;
 117     }
 118 
 119     /**
 120      * Circularly decrement i.
 121      */
 122     final int dec(int i) {
 123         return ((i == 0) ? items.length : i) - 1;
 124     }
 125 
 126     @SuppressWarnings("unchecked")
 127     static <E> E cast(Object item) {
 128         return (E) item;
 129     }
 130 
 131     /**
 132      * Returns item at index i.
 133      */
 134     @SuppressWarnings("unchecked")
 135     final E itemAt(int i) {
 136         return (E) items[i];
 137     }
 138 
 139     /**
 140      * Throws NullPointerException if argument is null.
 141      *
 142      * @param v the element
 143      */
 144     private static void checkNotNull(Object v) {
 145         if (v == null)
 146             throw new NullPointerException();
 147     }
 148 
 149     /**
 150      * Inserts element at current put position, advances, and signals.
 151      * Call only when holding lock.
 152      */
 153     private void insert(E x) {
 154         items[putIndex] = x;
 155         putIndex = inc(putIndex);
 156         ++count;
 157         notEmpty.signal();
 158     }
 159 
 160     /**
 161      * Extracts element at current take position, advances, and signals.
 162      * Call only when holding lock.
 163      */
 164     private E extract() {
 165         final Object[] items = this.items;
 166         @SuppressWarnings("unchecked") E x = (E) items[takeIndex];
 167         items[takeIndex] = null;
 168         takeIndex = inc(takeIndex);
 169         --count;
 170         notFull.signal();
 171         return x;
 172     }
 173 
 174     /**
 175      * Deletes item at position i.
 176      * Utility for remove and iterator.remove.
 177      * Call only when holding lock.
 178      */
 179     void removeAt(int i) {
 180         final Object[] items = this.items;
 181         // if removing front item, just advance
 182         if (i == takeIndex) {
 183             items[takeIndex] = null;
 184             takeIndex = inc(takeIndex);
 185         } else {
 186             // slide over all others up through putIndex.


 631     }
 632 
 633     /**
 634      * @throws UnsupportedOperationException {@inheritDoc}
 635      * @throws ClassCastException            {@inheritDoc}
 636      * @throws NullPointerException          {@inheritDoc}
 637      * @throws IllegalArgumentException      {@inheritDoc}
 638      */
 639     public int drainTo(Collection<? super E> c) {
 640         checkNotNull(c);
 641         if (c == this)
 642             throw new IllegalArgumentException();
 643         final Object[] items = this.items;
 644         final ReentrantLock lock = this.lock;
 645         lock.lock();
 646         try {
 647             int i = takeIndex;
 648             int n = 0;
 649             int max = count;
 650             while (n < max) {
 651                 @SuppressWarnings("unchecked") E x = (E) items[i];
 652                 c.add(x);
 653                 items[i] = null;
 654                 i = inc(i);
 655                 ++n;
 656             }
 657             if (n > 0) {
 658                 count = 0;
 659                 putIndex = 0;
 660                 takeIndex = 0;
 661                 notFull.signalAll();
 662             }
 663             return n;
 664         } finally {
 665             lock.unlock();
 666         }
 667     }
 668 
 669     /**
 670      * @throws UnsupportedOperationException {@inheritDoc}
 671      * @throws ClassCastException            {@inheritDoc}
 672      * @throws NullPointerException          {@inheritDoc}
 673      * @throws IllegalArgumentException      {@inheritDoc}
 674      */
 675     public int drainTo(Collection<? super E> c, int maxElements) {
 676         checkNotNull(c);
 677         if (c == this)
 678             throw new IllegalArgumentException();
 679         if (maxElements <= 0)
 680             return 0;
 681         final Object[] items = this.items;
 682         final ReentrantLock lock = this.lock;
 683         lock.lock();
 684         try {
 685             int i = takeIndex;
 686             int n = 0;
 687             int max = (maxElements < count) ? maxElements : count;
 688             while (n < max) {
 689                 @SuppressWarnings("unchecked") E x = (E) items[i];
 690                 c.add(x);
 691                 items[i] = null;
 692                 i = inc(i);
 693                 ++n;
 694             }
 695             if (n > 0) {
 696                 count -= n;
 697                 takeIndex = i;
 698                 notFull.signalAll();
 699             }
 700             return n;
 701         } finally {
 702             lock.unlock();
 703         }
 704     }
 705 
 706     /**
 707      * Returns an iterator over the elements in this queue in proper sequence.
 708      * The elements will be returned in order from first (head) to last (tail).
 709      *
 710      * <p>The returned {@code Iterator} is a "weakly consistent" iterator that