diff --git a/jdk/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java b/jdk/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java index fe91686627d..9a0346f6a52 100644 --- a/jdk/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/ArrayBlockingQueue.java @@ -757,12 +757,8 @@ public class ArrayBlockingQueue extends AbstractQueue * Returns an iterator over the elements in this queue in proper sequence. * The elements will be returned in order from first (head) to last (tail). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this queue in proper sequence */ @@ -1396,9 +1392,26 @@ public class ArrayBlockingQueue extends AbstractQueue // } } + /** + * Returns a {@link Spliterator} over the elements in this queue. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} implements {@code trySplit} to permit limited + * parallelism. + * + * @return a {@code Spliterator} over the elements in this queue + * @since 1.8 + */ public Spliterator spliterator() { return Spliterators.spliterator (this, Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.CONCURRENT); } + } diff --git a/jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java b/jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java index f29ec1d4efb..1936e244946 100644 --- a/jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java +++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentHashMap.java @@ -43,7 +43,6 @@ import java.util.AbstractMap; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; -import java.util.ConcurrentModificationException; import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; @@ -94,14 +93,14 @@ import java.util.stream.Stream; * that key reporting the updated value.) For aggregate operations * such as {@code putAll} and {@code clear}, concurrent retrievals may * reflect insertion or removal of only some entries. Similarly, - * Iterators and Enumerations return elements reflecting the state of - * the hash table at some point at or since the creation of the + * Iterators, Spliterators and Enumerations return elements reflecting the + * state of the hash table at some point at or since the creation of the * iterator/enumeration. They do not throw {@link - * ConcurrentModificationException}. However, iterators are designed - * to be used by only one thread at a time. Bear in mind that the - * results of aggregate status methods including {@code size}, {@code - * isEmpty}, and {@code containsValue} are typically useful only when - * a map is not undergoing concurrent updates in other threads. + * java.util.ConcurrentModificationException ConcurrentModificationException}. + * However, iterators are designed to be used by only one thread at a time. + * Bear in mind that the results of aggregate status methods including + * {@code size}, {@code isEmpty}, and {@code containsValue} are typically + * useful only when a map is not undergoing concurrent updates in other threads. * Otherwise the results of these methods reflect transient states * that may be adequate for monitoring or estimation purposes, but not * for program control. @@ -1200,11 +1199,11 @@ public class ConcurrentHashMap extends AbstractMap * operations. It does not support the {@code add} or * {@code addAll} operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator - * that will never throw {@link ConcurrentModificationException}, - * and guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not guaranteed to) - * reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. + * + *

The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}. * * @return the set view */ @@ -1223,11 +1222,11 @@ public class ConcurrentHashMap extends AbstractMap * {@code retainAll}, and {@code clear} operations. It does not * support the {@code add} or {@code addAll} operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator - * that will never throw {@link ConcurrentModificationException}, - * and guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not guaranteed to) - * reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. + * + *

The view's {@code spliterator} reports {@link Spliterator#CONCURRENT} + * and {@link Spliterator#NONNULL}. * * @return the collection view */ @@ -1245,11 +1244,11 @@ public class ConcurrentHashMap extends AbstractMap * {@code removeAll}, {@code retainAll}, and {@code clear} * operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator - * that will never throw {@link ConcurrentModificationException}, - * and guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not guaranteed to) - * reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. + * + *

The view's {@code spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#DISTINCT}, and {@link Spliterator#NONNULL}. * * @return the set view */ @@ -4308,12 +4307,12 @@ public class ConcurrentHashMap extends AbstractMap // implementations below rely on concrete classes supplying these // abstract methods /** - * Returns a "weakly consistent" iterator that will never - * throw {@link ConcurrentModificationException}, and - * guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not - * guaranteed to) reflect any modifications subsequent to - * construction. + * Returns an iterator over the elements in this collection. + * + *

The returned iterator is + * weakly consistent. + * + * @return an iterator over the elements in this collection */ public abstract Iterator iterator(); public abstract boolean contains(Object o); diff --git a/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java b/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java index 6bb62f0f98c..5697c419470 100644 --- a/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java +++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedDeque.java @@ -55,12 +55,8 @@ import java.util.function.Consumer; * Like most other concurrent collection implementations, this class * does not permit the use of {@code null} elements. * - *

Iterators are weakly consistent, returning elements - * reflecting the state of the deque at some point at or since the - * creation of the iterator. They do not throw {@link - * java.util.ConcurrentModificationException - * ConcurrentModificationException}, and may proceed concurrently with - * other operations. + *

Iterators and spliterators are + * weakly consistent. * *

Beware that, unlike in most collections, the {@code size} method * is NOT a constant-time operation. Because of the @@ -1290,12 +1286,8 @@ public class ConcurrentLinkedDeque * Returns an iterator over the elements in this deque in proper sequence. * The elements will be returned in order from first (head) to last (tail). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this deque in proper sequence */ @@ -1308,12 +1300,8 @@ public class ConcurrentLinkedDeque * sequential order. The elements will be returned in order from * last (tail) to first (head). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this deque in reverse order */ @@ -1493,6 +1481,22 @@ public class ConcurrentLinkedDeque } } + /** + * Returns a {@link Spliterator} over the elements in this deque. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} implements {@code trySplit} to permit limited + * parallelism. + * + * @return a {@code Spliterator} over the elements in this deque + * @since 1.8 + */ public Spliterator spliterator() { return new CLDSpliterator(this); } @@ -1500,6 +1504,8 @@ public class ConcurrentLinkedDeque /** * Saves this deque to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData All of the elements (each an {@code E}) in * the proper order, followed by a null */ @@ -1522,6 +1528,10 @@ public class ConcurrentLinkedDeque /** * Reconstitutes this deque from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { diff --git a/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java b/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java index 0b562bc217d..62512ac41dc 100644 --- a/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentLinkedQueue.java @@ -654,12 +654,8 @@ public class ConcurrentLinkedQueue extends AbstractQueue * Returns an iterator over the elements in this queue in proper sequence. * The elements will be returned in order from first (head) to last (tail). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this queue in proper sequence */ @@ -749,6 +745,8 @@ public class ConcurrentLinkedQueue extends AbstractQueue /** * Saves this queue to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData All of the elements (each an {@code E}) in * the proper order, followed by a null */ @@ -771,6 +769,10 @@ public class ConcurrentLinkedQueue extends AbstractQueue /** * Reconstitutes this queue from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { @@ -881,6 +883,23 @@ public class ConcurrentLinkedQueue extends AbstractQueue } } + /** + * Returns a {@link Spliterator} over the elements in this queue. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} implements {@code trySplit} to permit limited + * parallelism. + * + * @return a {@code Spliterator} over the elements in this queue + * @since 1.8 + */ + @Override public Spliterator spliterator() { return new CLQSpliterator(this); } diff --git a/jdk/src/share/classes/java/util/concurrent/ConcurrentNavigableMap.java b/jdk/src/share/classes/java/util/concurrent/ConcurrentNavigableMap.java index 7f54eab7b4e..9d624f44e5a 100644 --- a/jdk/src/share/classes/java/util/concurrent/ConcurrentNavigableMap.java +++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentNavigableMap.java @@ -120,11 +120,8 @@ public interface ConcurrentNavigableMap * operations. It does not support the {@code add} or {@code addAll} * operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator - * that will never throw {@link ConcurrentModificationException}, - * and guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not guaranteed to) - * reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. * * @return a navigable set view of the keys in this map */ @@ -141,11 +138,8 @@ public interface ConcurrentNavigableMap * operations. It does not support the {@code add} or {@code addAll} * operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator - * that will never throw {@link ConcurrentModificationException}, - * and guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not guaranteed to) - * reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. * *

This method is equivalent to method {@code navigableKeySet}. * @@ -164,11 +158,8 @@ public interface ConcurrentNavigableMap * operations. It does not support the {@code add} or {@code addAll} * operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator - * that will never throw {@link ConcurrentModificationException}, - * and guarantees to traverse elements as they existed upon - * construction of the iterator, and may (but is not guaranteed to) - * reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. * * @return a reverse order navigable set view of the keys in this map */ diff --git a/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java b/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java index ff75003e4c2..62364d9f0ec 100644 --- a/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java +++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListMap.java @@ -71,12 +71,13 @@ import java.util.function.Function; * {@code containsKey}, {@code get}, {@code put} and * {@code remove} operations and their variants. Insertion, removal, * update, and access operations safely execute concurrently by - * multiple threads. Iterators are weakly consistent, returning - * elements reflecting the state of the map at some point at or since - * the creation of the iterator. They do not throw {@link - * java.util.ConcurrentModificationException ConcurrentModificationException}, - * and may proceed concurrently with other operations. Ascending key ordered - * views and their iterators are faster than descending ones. + * multiple threads. + * + *

Iterators and spliterators are + * weakly consistent. + * + *

Ascending key ordered views and their iterators are faster than + * descending ones. * *

All {@code Map.Entry} pairs returned by methods in this class * and its views represent snapshots of mappings at the time they were @@ -1804,8 +1805,18 @@ public class ConcurrentSkipListMap extends AbstractMap /** * Returns a {@link NavigableSet} view of the keys contained in this map. - * The set's iterator returns the keys in ascending order. - * The set is backed by the map, so changes to the map are + * + *

The set's iterator returns the keys in ascending order. + * The set's spliterator additionally reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#NONNULL}, {@link Spliterator#SORTED} and + * {@link Spliterator#ORDERED}, with an encounter order that is ascending + * key order. The spliterator's comparator (see + * {@link java.util.Spliterator#getComparator()}) is {@code null} if + * the map's comparator (see {@link #comparator()}) is {@code null}. + * Otherwise, the spliterator's comparator is the same as or imposes the + * same total ordering as the map's comparator. + * + *

The set is backed by the map, so changes to the map are * reflected in the set, and vice-versa. The set supports element * removal, which removes the corresponding mapping from the map, * via the {@code Iterator.remove}, {@code Set.remove}, @@ -1813,11 +1824,8 @@ public class ConcurrentSkipListMap extends AbstractMap * operations. It does not support the {@code add} or {@code addAll} * operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse elements - * as they existed upon construction of the iterator, and may (but is not - * guaranteed to) reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. * *

This method is equivalent to method {@code navigableKeySet}. * @@ -1835,9 +1843,13 @@ public class ConcurrentSkipListMap extends AbstractMap /** * Returns a {@link Collection} view of the values contained in this map. - * The collection's iterator returns the values in ascending order - * of the corresponding keys. - * The collection is backed by the map, so changes to the map are + *

The collection's iterator returns the values in ascending order + * of the corresponding keys. The collections's spliterator additionally + * reports {@link Spliterator#CONCURRENT}, {@link Spliterator#NONNULL} and + * {@link Spliterator#ORDERED}, with an encounter order that is ascending + * order of the corresponding keys. + * + *

The collection is backed by the map, so changes to the map are * reflected in the collection, and vice-versa. The collection * supports element removal, which removes the corresponding * mapping from the map, via the {@code Iterator.remove}, @@ -1845,11 +1857,8 @@ public class ConcurrentSkipListMap extends AbstractMap * {@code retainAll} and {@code clear} operations. It does not * support the {@code add} or {@code addAll} operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse elements - * as they existed upon construction of the iterator, and may (but is not - * guaranteed to) reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. */ public Collection values() { Values vs = values; @@ -1858,8 +1867,14 @@ public class ConcurrentSkipListMap extends AbstractMap /** * Returns a {@link Set} view of the mappings contained in this map. - * The set's iterator returns the entries in ascending key order. - * The set is backed by the map, so changes to the map are + * + *

The set's iterator returns the entries in ascending key order. The + * set's spliterator additionally reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#NONNULL}, {@link Spliterator#SORTED} and + * {@link Spliterator#ORDERED}, with an encounter order that is ascending + * key order. + * + *

The set is backed by the map, so changes to the map are * reflected in the set, and vice-versa. The set supports element * removal, which removes the corresponding mapping from the map, * via the {@code Iterator.remove}, {@code Set.remove}, @@ -1867,15 +1882,12 @@ public class ConcurrentSkipListMap extends AbstractMap * operations. It does not support the {@code add} or * {@code addAll} operations. * - *

The view's {@code iterator} is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse elements - * as they existed upon construction of the iterator, and may (but is not - * guaranteed to) reflect any modifications subsequent to construction. + *

The view's iterators and spliterators are + * weakly consistent. * - *

The {@code Map.Entry} elements returned by - * {@code iterator.next()} do not support the - * {@code setValue} operation. + *

The {@code Map.Entry} elements traversed by the {@code iterator} + * or {@code spliterator} do not support the {@code setValue} + * operation. * * @return a set view of the mappings contained in this map, * sorted in ascending key order diff --git a/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListSet.java b/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListSet.java index 4c41e388c8b..17989f8d3cc 100644 --- a/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListSet.java +++ b/jdk/src/share/classes/java/util/concurrent/ConcurrentSkipListSet.java @@ -57,12 +57,12 @@ import java.util.Spliterator; * cost for the {@code contains}, {@code add}, and {@code remove} * operations and their variants. Insertion, removal, and access * operations safely execute concurrently by multiple threads. - * Iterators are weakly consistent, returning elements - * reflecting the state of the set at some point at or since the - * creation of the iterator. They do not throw {@link - * java.util.ConcurrentModificationException}, and may proceed - * concurrently with other operations. Ascending ordered views and - * their iterators are faster than descending ones. + * + *

Iterators and spliterators are + * weakly consistent. + * + *

Ascending ordered views and their iterators are faster than + * descending ones. * *

Beware that, unlike in most collections, the {@code size} * method is not a constant-time operation. Because of the @@ -480,6 +480,24 @@ public class ConcurrentSkipListSet return new ConcurrentSkipListSet(m.descendingMap()); } + /** + * Returns a {@link Spliterator} over the elements in this set. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#NONNULL}, {@link Spliterator#DISTINCT}, + * {@link Spliterator#SORTED} and {@link Spliterator#ORDERED}, with an + * encounter order that is ascending order. Overriding implementations + * should document the reporting of additional characteristic values. + * + *

The spliterator's comparator (see + * {@link java.util.Spliterator#getComparator()}) is {@code null} if + * the set's comparator (see {@link #comparator()}) is {@code null}. + * Otherwise, the spliterator's comparator is the same as or imposes the + * same total ordering as the set's comparator. + * + * @return a {@code Spliterator} over the elements in this set + * @since 1.8 + */ @SuppressWarnings("unchecked") public Spliterator spliterator() { if (m instanceof ConcurrentSkipListMap) diff --git a/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java b/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java index 2ea321100c3..fae67b2b2d0 100644 --- a/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java +++ b/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArrayList.java @@ -952,6 +952,8 @@ public class CopyOnWriteArrayList /** * Saves this list to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData The length of the array backing the list is emitted * (int), followed by all of its elements (each an Object) * in the proper order. @@ -972,6 +974,10 @@ public class CopyOnWriteArrayList /** * Reconstitutes this list from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { @@ -1092,15 +1098,29 @@ public class CopyOnWriteArrayList * * @throws IndexOutOfBoundsException {@inheritDoc} */ - public ListIterator listIterator(final int index) { + public ListIterator listIterator(int index) { Object[] elements = getArray(); int len = elements.length; - if (index<0 || index>len) + if (index < 0 || index > len) throw new IndexOutOfBoundsException("Index: "+index); return new COWIterator(elements, index); } + /** + * Returns a {@link Spliterator} over the elements in this list. + * + *

The {@code Spliterator} reports {@link Spliterator#IMMUTABLE}, + * {@link Spliterator#ORDERED}, {@link Spliterator#SIZED}, and + * {@link Spliterator#SUBSIZED}. + * + *

The spliterator provides a snapshot of the state of the list + * when the spliterator was constructed. No synchronization is needed while + * operating on the spliterator. + * + * @return a {@code Spliterator} over the elements in this list + * @since 1.8 + */ public Spliterator spliterator() { return Spliterators.spliterator (getArray(), Spliterator.IMMUTABLE | Spliterator.ORDERED); @@ -1257,7 +1277,7 @@ public class CopyOnWriteArrayList // only call this holding l's lock private void rangeCheck(int index) { - if (index<0 || index>=size) + if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: "+index+ ",Size: "+size); } @@ -1304,7 +1324,7 @@ public class CopyOnWriteArrayList lock.lock(); try { checkForComodification(); - if (index<0 || index>size) + if (index < 0 || index > size) throw new IndexOutOfBoundsException(); l.add(index+offset, element); expectedArray = l.getArray(); @@ -1361,12 +1381,12 @@ public class CopyOnWriteArrayList } } - public ListIterator listIterator(final int index) { + public ListIterator listIterator(int index) { final ReentrantLock lock = l.lock; lock.lock(); try { checkForComodification(); - if (index<0 || index>size) + if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index: "+index+ ", Size: "+size); return new COWSubListIterator(l, index, offset, size); @@ -1380,7 +1400,7 @@ public class CopyOnWriteArrayList lock.lock(); try { checkForComodification(); - if (fromIndex<0 || toIndex>size) + if (fromIndex < 0 || toIndex > size) throw new IndexOutOfBoundsException(); return new COWSubList(l, fromIndex + offset, toIndex + offset); @@ -1580,6 +1600,7 @@ public class CopyOnWriteArrayList return Spliterators.spliterator (a, lo, hi, Spliterator.IMMUTABLE | Spliterator.ORDERED); } + } private static class COWSubListIterator implements ListIterator { diff --git a/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java b/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java index ffb9668647c..059cceb103a 100644 --- a/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java +++ b/jdk/src/share/classes/java/util/concurrent/CopyOnWriteArraySet.java @@ -404,6 +404,21 @@ public class CopyOnWriteArraySet extends AbstractSet al.forEach(action); } + /** + * Returns a {@link Spliterator} over the elements in this set in the order + * in which these elements were added. + * + *

The {@code Spliterator} reports {@link Spliterator#IMMUTABLE}, + * {@link Spliterator#DISTINCT}, {@link Spliterator#SIZED}, and + * {@link Spliterator#SUBSIZED}. + * + *

The spliterator provides a snapshot of the state of the set + * when the spliterator was constructed. No synchronization is needed while + * operating on the spliterator. + * + * @return a {@code Spliterator} over the elements in this set + * @since 1.8 + */ public Spliterator spliterator() { return Spliterators.spliterator (al.getArray(), Spliterator.IMMUTABLE | Spliterator.DISTINCT); diff --git a/jdk/src/share/classes/java/util/concurrent/DelayQueue.java b/jdk/src/share/classes/java/util/concurrent/DelayQueue.java index 610fb717ac8..b729265a016 100644 --- a/jdk/src/share/classes/java/util/concurrent/DelayQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/DelayQueue.java @@ -512,12 +512,8 @@ public class DelayQueue extends AbstractQueue * unexpired) in this queue. The iterator does not return the * elements in any particular order. * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this queue */ diff --git a/jdk/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java b/jdk/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java index df688a57b06..73d3da049ec 100644 --- a/jdk/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java +++ b/jdk/src/share/classes/java/util/concurrent/LinkedBlockingDeque.java @@ -1008,12 +1008,8 @@ public class LinkedBlockingDeque * Returns an iterator over the elements in this deque in proper sequence. * The elements will be returned in order from first (head) to last (tail). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this deque in proper sequence */ @@ -1026,12 +1022,8 @@ public class LinkedBlockingDeque * sequential order. The elements will be returned in order from * last (tail) to first (head). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this deque in reverse order */ @@ -1270,6 +1262,22 @@ public class LinkedBlockingDeque } } + /** + * Returns a {@link Spliterator} over the elements in this deque. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} implements {@code trySplit} to permit limited + * parallelism. + * + * @return a {@code Spliterator} over the elements in this deque + * @since 1.8 + */ public Spliterator spliterator() { return new LBDSpliterator(this); } @@ -1277,6 +1285,8 @@ public class LinkedBlockingDeque /** * Saves this deque to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData The capacity (int), followed by elements (each an * {@code Object}) in the proper order, followed by a null */ @@ -1299,6 +1309,10 @@ public class LinkedBlockingDeque /** * Reconstitutes this deque from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { diff --git a/jdk/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java b/jdk/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java index 05bf7cc22de..d7a1587146a 100644 --- a/jdk/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/LinkedBlockingQueue.java @@ -766,12 +766,8 @@ public class LinkedBlockingQueue extends AbstractQueue * Returns an iterator over the elements in this queue in proper sequence. * The elements will be returned in order from first (head) to last (tail). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this queue in proper sequence */ @@ -973,6 +969,22 @@ public class LinkedBlockingQueue extends AbstractQueue } } + /** + * Returns a {@link Spliterator} over the elements in this queue. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} implements {@code trySplit} to permit limited + * parallelism. + * + * @return a {@code Spliterator} over the elements in this queue + * @since 1.8 + */ public Spliterator spliterator() { return new LBQSpliterator(this); } @@ -980,6 +992,8 @@ public class LinkedBlockingQueue extends AbstractQueue /** * Saves this queue to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData The capacity is emitted (int), followed by all of * its elements (each an {@code Object}) in the proper order, * followed by a null @@ -1005,6 +1019,10 @@ public class LinkedBlockingQueue extends AbstractQueue /** * Reconstitutes this queue from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { diff --git a/jdk/src/share/classes/java/util/concurrent/LinkedTransferQueue.java b/jdk/src/share/classes/java/util/concurrent/LinkedTransferQueue.java index 7dbb368cc52..5ebd04923d4 100644 --- a/jdk/src/share/classes/java/util/concurrent/LinkedTransferQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/LinkedTransferQueue.java @@ -40,6 +40,7 @@ import java.util.Collection; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Queue; +import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; import java.util.Spliterator; import java.util.Spliterators; @@ -1018,6 +1019,22 @@ public class LinkedTransferQueue extends AbstractQueue } } + /** + * Returns a {@link Spliterator} over the elements in this queue. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#CONCURRENT}, + * {@link Spliterator#ORDERED}, and {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} implements {@code trySplit} to permit limited + * parallelism. + * + * @return a {@code Spliterator} over the elements in this queue + * @since 1.8 + */ public Spliterator spliterator() { return new LTQSpliterator(this); } @@ -1301,12 +1318,8 @@ public class LinkedTransferQueue extends AbstractQueue * Returns an iterator over the elements in this queue in proper sequence. * The elements will be returned in order from first (head) to last (tail). * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this queue in proper sequence */ @@ -1407,6 +1420,8 @@ public class LinkedTransferQueue extends AbstractQueue /** * Saves this queue to a stream (that is, serializes it). * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs * @serialData All of the elements (each an {@code E}) in * the proper order, followed by a null */ @@ -1421,6 +1436,10 @@ public class LinkedTransferQueue extends AbstractQueue /** * Reconstitutes this queue from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { diff --git a/jdk/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java b/jdk/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java index fdf9c71af93..db888250c10 100644 --- a/jdk/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/PriorityBlockingQueue.java @@ -229,7 +229,7 @@ public class PriorityBlockingQueue extends AbstractQueue /** * Creates a {@code PriorityBlockingQueue} containing the elements * in the specified collection. If the specified collection is a - * {@link SortedSet} or a {@link PriorityQueue}, this + * {@link SortedSet} or a {@link PriorityQueue}, this * priority queue will be ordered according to the same ordering. * Otherwise, this priority queue will be ordered according to the * {@linkplain Comparable natural ordering} of its elements. @@ -864,12 +864,8 @@ public class PriorityBlockingQueue extends AbstractQueue * Returns an iterator over the elements in this queue. The * iterator does not return the elements in any particular order. * - *

The returned iterator is a "weakly consistent" iterator that - * will never throw {@link java.util.ConcurrentModificationException - * ConcurrentModificationException}, and guarantees to traverse - * elements as they existed upon construction of the iterator, and - * may (but is not guaranteed to) reflect any modifications - * subsequent to construction. + *

The returned iterator is + * weakly consistent. * * @return an iterator over the elements in this queue */ @@ -915,6 +911,9 @@ public class PriorityBlockingQueue extends AbstractQueue * For compatibility with previous version of this class, elements * are first copied to a java.util.PriorityQueue, which is then * serialized. + * + * @param s the stream + * @throws java.io.IOException if an I/O error occurs */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { @@ -932,6 +931,10 @@ public class PriorityBlockingQueue extends AbstractQueue /** * Reconstitutes this queue from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { @@ -1005,6 +1008,21 @@ public class PriorityBlockingQueue extends AbstractQueue } } + /** + * Returns a {@link Spliterator} over the elements in this queue. + * + *

The returned spliterator is + * weakly consistent. + * + *

The {@code Spliterator} reports {@link Spliterator#SIZED} and + * {@link Spliterator#NONNULL}. + * + * @implNote + * The {@code Spliterator} additionally reports {@link Spliterator#SUBSIZED}. + * + * @return a {@code Spliterator} over the elements in this queue + * @since 1.8 + */ public Spliterator spliterator() { return new PBQSpliterator(this, null, 0, -1); } diff --git a/jdk/src/share/classes/java/util/concurrent/SynchronousQueue.java b/jdk/src/share/classes/java/util/concurrent/SynchronousQueue.java index 97b53049716..fdc42f926ee 100644 --- a/jdk/src/share/classes/java/util/concurrent/SynchronousQueue.java +++ b/jdk/src/share/classes/java/util/concurrent/SynchronousQueue.java @@ -38,6 +38,8 @@ package java.util.concurrent; import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.ReentrantLock; import java.util.*; +import java.util.Spliterator; +import java.util.Spliterators; /** * A {@linkplain BlockingQueue blocking queue} in which each insert @@ -1062,21 +1064,17 @@ public class SynchronousQueue extends AbstractQueue * * @return an empty iterator */ - @SuppressWarnings("unchecked") public Iterator iterator() { - return (Iterator) EmptyIterator.EMPTY_ITERATOR; - } - - // Replicated from a previous version of Collections - private static class EmptyIterator implements Iterator { - static final EmptyIterator EMPTY_ITERATOR - = new EmptyIterator(); - - public boolean hasNext() { return false; } - public E next() { throw new NoSuchElementException(); } - public void remove() { throw new IllegalStateException(); } + return Collections.emptyIterator(); } + /** + * Returns an empty spliterator in which calls to + * {@link java.util.Spliterator#trySplit()} always return {@code null}. + * + * @return an empty spliterator + * @since 1.8 + */ public Spliterator spliterator() { return Spliterators.emptySpliterator(); } @@ -1163,6 +1161,8 @@ public class SynchronousQueue extends AbstractQueue /** * Saves this queue to a stream (that is, serializes it). + * @param s the stream + * @throws java.io.IOException if an I/O error occurs */ private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { @@ -1182,8 +1182,12 @@ public class SynchronousQueue extends AbstractQueue /** * Reconstitutes this queue from a stream (that is, deserializes it). + * @param s the stream + * @throws ClassNotFoundException if the class of a serialized object + * could not be found + * @throws java.io.IOException if an I/O error occurs */ - private void readObject(final java.io.ObjectInputStream s) + private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { s.defaultReadObject(); if (waitingProducers instanceof FifoWaitQueue) diff --git a/jdk/src/share/classes/java/util/concurrent/package-info.java b/jdk/src/share/classes/java/util/concurrent/package-info.java index b236c290b10..585c769fc7d 100644 --- a/jdk/src/share/classes/java/util/concurrent/package-info.java +++ b/jdk/src/share/classes/java/util/concurrent/package-info.java @@ -210,13 +210,19 @@ * collections are unshared, or are accessible only when * holding other locks. * - *

Most concurrent Collection implementations (including most - * Queues) also differ from the usual java.util conventions in that - * their Iterators provide weakly consistent rather than - * fast-fail traversal. A weakly consistent iterator is thread-safe, - * but does not necessarily freeze the collection while iterating, so - * it may (or may not) reflect any updates since the iterator was - * created. + *

Most concurrent Collection implementations + * (including most Queues) also differ from the usual {@code java.util} + * conventions in that their {@linkplain java.util.Iterator Iterators} + * and {@linkplain java.util.Spliterator Spliterators} provide + * weakly consistent rather than fast-fail traversal: + *

    + *
  • they may proceed concurrently with other operations + *
  • they will never throw {@link java.util.ConcurrentModificationException + * ConcurrentModificationException} + *
  • they are guaranteed to traverse elements as they existed upon + * construction exactly once, and may (but are not guaranteed to) + * reflect any modifications subsequent to construction. + *
* *

Memory Consistency Properties

*