< prev index next >

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

Print this page




1424      *
1425      * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
1426      * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
1427      *
1428      * @implNote
1429      * The {@code Spliterator} implements {@code trySplit} to permit limited
1430      * parallelism.
1431      *
1432      * @return a {@code Spliterator} over the elements in this queue
1433      * @since 1.8
1434      */
1435     public Spliterator<E> spliterator() {
1436         return Spliterators.spliterator
1437             (this, (Spliterator.ORDERED |
1438                     Spliterator.NONNULL |
1439                     Spliterator.CONCURRENT));
1440     }
1441 
1442     /**
1443      * @throws NullPointerException {@inheritDoc}

1444      */
1445     public void forEach(Consumer<? super E> action) {
1446         Objects.requireNonNull(action);
1447         final ReentrantLock lock = this.lock;
1448         lock.lock();
1449         try {
1450             if (count > 0) {
1451                 final Object[] items = this.items;
1452                 for (int i = takeIndex, end = putIndex,
1453                          to = (i < end) ? end : items.length;
1454                      ; i = 0, to = end) {
1455                     for (; i < to; i++)
1456                         action.accept(itemAt(items, i));
1457                     if (to == end) break;
1458                 }
1459             }
1460         } finally {
1461             lock.unlock();
1462         }
1463     }
1464 
1465     /**
1466      * @throws NullPointerException {@inheritDoc}

1467      */
1468     public boolean removeIf(Predicate<? super E> filter) {
1469         Objects.requireNonNull(filter);
1470         return bulkRemove(filter);
1471     }
1472 
1473     /**
1474      * @throws NullPointerException {@inheritDoc}

1475      */
1476     public boolean removeAll(Collection<?> c) {
1477         Objects.requireNonNull(c);
1478         return bulkRemove(e -> c.contains(e));
1479     }
1480 
1481     /**
1482      * @throws NullPointerException {@inheritDoc}

1483      */
1484     public boolean retainAll(Collection<?> c) {
1485         Objects.requireNonNull(c);
1486         return bulkRemove(e -> !c.contains(e));
1487     }
1488 
1489     /** Implementation of bulk remove methods. */
1490     private boolean bulkRemove(Predicate<? super E> filter) {
1491         final ReentrantLock lock = this.lock;
1492         lock.lock();
1493         try {
1494             if (itrs == null) { // check for active iterators
1495                 if (count > 0) {
1496                     final Object[] items = this.items;
1497                     // Optimize for initial run of survivors
1498                     for (int i = takeIndex, end = putIndex,
1499                              to = (i < end) ? end : items.length;
1500                          ; i = 0, to = end) {
1501                         for (; i < to; i++)
1502                             if (filter.test(itemAt(items, i)))




1424      *
1425      * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
1426      * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
1427      *
1428      * @implNote
1429      * The {@code Spliterator} implements {@code trySplit} to permit limited
1430      * parallelism.
1431      *
1432      * @return a {@code Spliterator} over the elements in this queue
1433      * @since 1.8
1434      */
1435     public Spliterator<E> spliterator() {
1436         return Spliterators.spliterator
1437             (this, (Spliterator.ORDERED |
1438                     Spliterator.NONNULL |
1439                     Spliterator.CONCURRENT));
1440     }
1441 
1442     /**
1443      * @throws NullPointerException {@inheritDoc}
1444      * @since 9
1445      */
1446     public void forEach(Consumer<? super E> action) {
1447         Objects.requireNonNull(action);
1448         final ReentrantLock lock = this.lock;
1449         lock.lock();
1450         try {
1451             if (count > 0) {
1452                 final Object[] items = this.items;
1453                 for (int i = takeIndex, end = putIndex,
1454                          to = (i < end) ? end : items.length;
1455                      ; i = 0, to = end) {
1456                     for (; i < to; i++)
1457                         action.accept(itemAt(items, i));
1458                     if (to == end) break;
1459                 }
1460             }
1461         } finally {
1462             lock.unlock();
1463         }
1464     }
1465 
1466     /**
1467      * @throws NullPointerException {@inheritDoc}
1468      * @since 9
1469      */
1470     public boolean removeIf(Predicate<? super E> filter) {
1471         Objects.requireNonNull(filter);
1472         return bulkRemove(filter);
1473     }
1474 
1475     /**
1476      * @throws NullPointerException {@inheritDoc}
1477      * @since 9
1478      */
1479     public boolean removeAll(Collection<?> c) {
1480         Objects.requireNonNull(c);
1481         return bulkRemove(e -> c.contains(e));
1482     }
1483 
1484     /**
1485      * @throws NullPointerException {@inheritDoc}
1486      * @since 9
1487      */
1488     public boolean retainAll(Collection<?> c) {
1489         Objects.requireNonNull(c);
1490         return bulkRemove(e -> !c.contains(e));
1491     }
1492 
1493     /** Implementation of bulk remove methods. */
1494     private boolean bulkRemove(Predicate<? super E> filter) {
1495         final ReentrantLock lock = this.lock;
1496         lock.lock();
1497         try {
1498             if (itrs == null) { // check for active iterators
1499                 if (count > 0) {
1500                     final Object[] items = this.items;
1501                     // Optimize for initial run of survivors
1502                     for (int i = takeIndex, end = putIndex,
1503                              to = (i < end) ? end : items.length;
1504                          ; i = 0, to = end) {
1505                         for (; i < to; i++)
1506                             if (filter.test(itemAt(items, i)))


< prev index next >