< prev index next >

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

Print this page




 941      * <p>The returned spliterator is
 942      * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
 943      *
 944      * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
 945      * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
 946      *
 947      * @implNote
 948      * The {@code Spliterator} implements {@code trySplit} to permit limited
 949      * parallelism.
 950      *
 951      * @return a {@code Spliterator} over the elements in this queue
 952      * @since 1.8
 953      */
 954     @Override
 955     public Spliterator<E> spliterator() {
 956         return new CLQSpliterator();
 957     }
 958 
 959     /**
 960      * @throws NullPointerException {@inheritDoc}

 961      */
 962     public boolean removeIf(Predicate<? super E> filter) {
 963         Objects.requireNonNull(filter);
 964         return bulkRemove(filter);
 965     }
 966 
 967     /**
 968      * @throws NullPointerException {@inheritDoc}

 969      */
 970     public boolean removeAll(Collection<?> c) {
 971         Objects.requireNonNull(c);
 972         return bulkRemove(e -> c.contains(e));
 973     }
 974 
 975     /**
 976      * @throws NullPointerException {@inheritDoc}

 977      */
 978     public boolean retainAll(Collection<?> c) {
 979         Objects.requireNonNull(c);
 980         return bulkRemove(e -> !c.contains(e));
 981     }
 982 
 983     public void clear() {
 984         bulkRemove(e -> true);
 985     }
 986 
 987     /**
 988      * Tolerate this many consecutive dead nodes before CAS-collapsing.
 989      * Amortized cost of clear() is (1 + 1/MAX_HOPS) CASes per element.
 990      */
 991     private static final int MAX_HOPS = 8;
 992 
 993     /** Implementation of bulk remove methods. */
 994     private boolean bulkRemove(Predicate<? super E> filter) {
 995         boolean removed = false;
 996         restartFromHead: for (;;) {


1031      */
1032     void forEachFrom(Consumer<? super E> action, Node<E> p) {
1033         for (Node<E> pred = null; p != null; ) {
1034             Node<E> q = p.next;
1035             final E item;
1036             if ((item = p.item) != null) {
1037                 action.accept(item);
1038                 pred = p; p = q; continue;
1039             }
1040             for (Node<E> c = p;; q = p.next) {
1041                 if (q == null || q.item != null) {
1042                     pred = skipDeadNodes(pred, c, p, q); p = q; break;
1043                 }
1044                 if (p == (p = q)) { pred = null; p = head; break; }
1045             }
1046         }
1047     }
1048 
1049     /**
1050      * @throws NullPointerException {@inheritDoc}

1051      */
1052     public void forEach(Consumer<? super E> action) {
1053         Objects.requireNonNull(action);
1054         forEachFrom(action, head);
1055     }
1056 
1057     // VarHandle mechanics
1058     private static final VarHandle HEAD;
1059     private static final VarHandle TAIL;
1060     static final VarHandle ITEM;
1061     static final VarHandle NEXT;
1062     static {
1063         try {
1064             MethodHandles.Lookup l = MethodHandles.lookup();
1065             HEAD = l.findVarHandle(ConcurrentLinkedQueue.class, "head",
1066                                    Node.class);
1067             TAIL = l.findVarHandle(ConcurrentLinkedQueue.class, "tail",
1068                                    Node.class);
1069             ITEM = l.findVarHandle(Node.class, "item", Object.class);
1070             NEXT = l.findVarHandle(Node.class, "next", Node.class);


 941      * <p>The returned spliterator is
 942      * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
 943      *
 944      * <p>The {@code Spliterator} reports {@link Spliterator#CONCURRENT},
 945      * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}.
 946      *
 947      * @implNote
 948      * The {@code Spliterator} implements {@code trySplit} to permit limited
 949      * parallelism.
 950      *
 951      * @return a {@code Spliterator} over the elements in this queue
 952      * @since 1.8
 953      */
 954     @Override
 955     public Spliterator<E> spliterator() {
 956         return new CLQSpliterator();
 957     }
 958 
 959     /**
 960      * @throws NullPointerException {@inheritDoc}
 961      * @since 9
 962      */
 963     public boolean removeIf(Predicate<? super E> filter) {
 964         Objects.requireNonNull(filter);
 965         return bulkRemove(filter);
 966     }
 967 
 968     /**
 969      * @throws NullPointerException {@inheritDoc}
 970      * @since 9
 971      */
 972     public boolean removeAll(Collection<?> c) {
 973         Objects.requireNonNull(c);
 974         return bulkRemove(e -> c.contains(e));
 975     }
 976 
 977     /**
 978      * @throws NullPointerException {@inheritDoc}
 979      * @since 9
 980      */
 981     public boolean retainAll(Collection<?> c) {
 982         Objects.requireNonNull(c);
 983         return bulkRemove(e -> !c.contains(e));
 984     }
 985 
 986     public void clear() {
 987         bulkRemove(e -> true);
 988     }
 989 
 990     /**
 991      * Tolerate this many consecutive dead nodes before CAS-collapsing.
 992      * Amortized cost of clear() is (1 + 1/MAX_HOPS) CASes per element.
 993      */
 994     private static final int MAX_HOPS = 8;
 995 
 996     /** Implementation of bulk remove methods. */
 997     private boolean bulkRemove(Predicate<? super E> filter) {
 998         boolean removed = false;
 999         restartFromHead: for (;;) {


1034      */
1035     void forEachFrom(Consumer<? super E> action, Node<E> p) {
1036         for (Node<E> pred = null; p != null; ) {
1037             Node<E> q = p.next;
1038             final E item;
1039             if ((item = p.item) != null) {
1040                 action.accept(item);
1041                 pred = p; p = q; continue;
1042             }
1043             for (Node<E> c = p;; q = p.next) {
1044                 if (q == null || q.item != null) {
1045                     pred = skipDeadNodes(pred, c, p, q); p = q; break;
1046                 }
1047                 if (p == (p = q)) { pred = null; p = head; break; }
1048             }
1049         }
1050     }
1051 
1052     /**
1053      * @throws NullPointerException {@inheritDoc}
1054      * @since 9
1055      */
1056     public void forEach(Consumer<? super E> action) {
1057         Objects.requireNonNull(action);
1058         forEachFrom(action, head);
1059     }
1060 
1061     // VarHandle mechanics
1062     private static final VarHandle HEAD;
1063     private static final VarHandle TAIL;
1064     static final VarHandle ITEM;
1065     static final VarHandle NEXT;
1066     static {
1067         try {
1068             MethodHandles.Lookup l = MethodHandles.lookup();
1069             HEAD = l.findVarHandle(ConcurrentLinkedQueue.class, "head",
1070                                    Node.class);
1071             TAIL = l.findVarHandle(ConcurrentLinkedQueue.class, "tail",
1072                                    Node.class);
1073             ITEM = l.findVarHandle(Node.class, "item", Object.class);
1074             NEXT = l.findVarHandle(Node.class, "next", Node.class);
< prev index next >