< prev index next >

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

Print this page




 957      *
 958      * <p>The returned spliterator is
 959      * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
 960      *
 961      * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
 962      * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
 963      *
 964      * @implNote
 965      * The {@code Spliterator} implements {@code trySplit} to permit limited
 966      * parallelism.
 967      *
 968      * @return a {@code Spliterator} over the elements in this queue
 969      * @since 1.8
 970      */
 971     public Spliterator<E> spliterator() {
 972         return new LBQSpliterator();
 973     }
 974 
 975     /**
 976      * @throws NullPointerException {@inheritDoc}

 977      */
 978     public void forEach(Consumer<? super E> action) {
 979         Objects.requireNonNull(action);
 980         forEachFrom(action, null);
 981     }
 982 
 983     /**
 984      * Runs action on each element found during a traversal starting at p.
 985      * If p is null, traversal starts at head.
 986      */
 987     void forEachFrom(Consumer<? super E> action, Node<E> p) {
 988         // Extract batches of elements while holding the lock; then
 989         // run the action on the elements while not
 990         final int batchSize = 64;       // max number of elements per batch
 991         Object[] es = null;             // container for batch of elements
 992         int n, len = 0;
 993         do {
 994             fullyLock();
 995             try {
 996                 if (es == null) {


 998                     for (Node<E> q = p; q != null; q = succ(q))
 999                         if (q.item != null && ++len == batchSize)
1000                             break;
1001                     es = new Object[len];
1002                 }
1003                 for (n = 0; p != null && n < len; p = succ(p))
1004                     if ((es[n] = p.item) != null)
1005                         n++;
1006             } finally {
1007                 fullyUnlock();
1008             }
1009             for (int i = 0; i < n; i++) {
1010                 @SuppressWarnings("unchecked") E e = (E) es[i];
1011                 action.accept(e);
1012             }
1013         } while (n > 0 && p != null);
1014     }
1015 
1016     /**
1017      * @throws NullPointerException {@inheritDoc}

1018      */
1019     public boolean removeIf(Predicate<? super E> filter) {
1020         Objects.requireNonNull(filter);
1021         return bulkRemove(filter);
1022     }
1023 
1024     /**
1025      * @throws NullPointerException {@inheritDoc}

1026      */
1027     public boolean removeAll(Collection<?> c) {
1028         Objects.requireNonNull(c);
1029         return bulkRemove(e -> c.contains(e));
1030     }
1031 
1032     /**
1033      * @throws NullPointerException {@inheritDoc}

1034      */
1035     public boolean retainAll(Collection<?> c) {
1036         Objects.requireNonNull(c);
1037         return bulkRemove(e -> !c.contains(e));
1038     }
1039 
1040     /**
1041      * Returns the predecessor of live node p, given a node that was
1042      * once a live ancestor of p (or head); allows unlinking of p.
1043      */
1044     Node<E> findPred(Node<E> p, Node<E> ancestor) {
1045         // assert p.item != null;
1046         if (ancestor.item == null)
1047             ancestor = head;
1048         // Fails with NPE if precondition not satisfied
1049         for (Node<E> q; (q = ancestor.next) != p; )
1050             ancestor = q;
1051         return ancestor;
1052     }
1053 




 957      *
 958      * <p>The returned spliterator is
 959      * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
 960      *
 961      * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
 962      * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
 963      *
 964      * @implNote
 965      * The {@code Spliterator} implements {@code trySplit} to permit limited
 966      * parallelism.
 967      *
 968      * @return a {@code Spliterator} over the elements in this queue
 969      * @since 1.8
 970      */
 971     public Spliterator<E> spliterator() {
 972         return new LBQSpliterator();
 973     }
 974 
 975     /**
 976      * @throws NullPointerException {@inheritDoc}
 977      * @since 9
 978      */
 979     public void forEach(Consumer<? super E> action) {
 980         Objects.requireNonNull(action);
 981         forEachFrom(action, null);
 982     }
 983 
 984     /**
 985      * Runs action on each element found during a traversal starting at p.
 986      * If p is null, traversal starts at head.
 987      */
 988     void forEachFrom(Consumer<? super E> action, Node<E> p) {
 989         // Extract batches of elements while holding the lock; then
 990         // run the action on the elements while not
 991         final int batchSize = 64;       // max number of elements per batch
 992         Object[] es = null;             // container for batch of elements
 993         int n, len = 0;
 994         do {
 995             fullyLock();
 996             try {
 997                 if (es == null) {


 999                     for (Node<E> q = p; q != null; q = succ(q))
1000                         if (q.item != null && ++len == batchSize)
1001                             break;
1002                     es = new Object[len];
1003                 }
1004                 for (n = 0; p != null && n < len; p = succ(p))
1005                     if ((es[n] = p.item) != null)
1006                         n++;
1007             } finally {
1008                 fullyUnlock();
1009             }
1010             for (int i = 0; i < n; i++) {
1011                 @SuppressWarnings("unchecked") E e = (E) es[i];
1012                 action.accept(e);
1013             }
1014         } while (n > 0 && p != null);
1015     }
1016 
1017     /**
1018      * @throws NullPointerException {@inheritDoc}
1019      * @since 9
1020      */
1021     public boolean removeIf(Predicate<? super E> filter) {
1022         Objects.requireNonNull(filter);
1023         return bulkRemove(filter);
1024     }
1025 
1026     /**
1027      * @throws NullPointerException {@inheritDoc}
1028      * @since 9
1029      */
1030     public boolean removeAll(Collection<?> c) {
1031         Objects.requireNonNull(c);
1032         return bulkRemove(e -> c.contains(e));
1033     }
1034 
1035     /**
1036      * @throws NullPointerException {@inheritDoc}
1037      * @since 9
1038      */
1039     public boolean retainAll(Collection<?> c) {
1040         Objects.requireNonNull(c);
1041         return bulkRemove(e -> !c.contains(e));
1042     }
1043 
1044     /**
1045      * Returns the predecessor of live node p, given a node that was
1046      * once a live ancestor of p (or head); allows unlinking of p.
1047      */
1048     Node<E> findPred(Node<E> p, Node<E> ancestor) {
1049         // assert p.item != null;
1050         if (ancestor.item == null)
1051             ancestor = head;
1052         // Fails with NPE if precondition not satisfied
1053         for (Node<E> q; (q = ancestor.next) != p; )
1054             ancestor = q;
1055         return ancestor;
1056     }
1057 


< prev index next >