< prev index next >

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

Print this page




  34 
  35 package java.util.concurrent;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.lang.reflect.Field;
  39 import java.util.Arrays;
  40 import java.util.Collection;
  41 import java.util.Comparator;
  42 import java.util.ConcurrentModificationException;
  43 import java.util.Iterator;
  44 import java.util.List;
  45 import java.util.ListIterator;
  46 import java.util.NoSuchElementException;
  47 import java.util.Objects;
  48 import java.util.RandomAccess;
  49 import java.util.Spliterator;
  50 import java.util.Spliterators;
  51 import java.util.function.Consumer;
  52 import java.util.function.Predicate;
  53 import java.util.function.UnaryOperator;
  54 import jdk.internal.misc.SharedSecrets;
  55 
  56 /**
  57  * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
  58  * operations ({@code add}, {@code set}, and so on) are implemented by
  59  * making a fresh copy of the underlying array.
  60  *
  61  * <p>This is ordinarily too costly, but may be <em>more</em> efficient
  62  * than alternatives when traversal operations vastly outnumber
  63  * mutations, and is useful when you cannot or don't want to
  64  * synchronize traversals, yet need to preclude interference among
  65  * concurrent threads.  The "snapshot" style iterator method uses a
  66  * reference to the state of the array at the point that the iterator
  67  * was created. This array never changes during the lifetime of the
  68  * iterator, so interference is impossible and the iterator is
  69  * guaranteed not to throw {@code ConcurrentModificationException}.
  70  * The iterator will not reflect additions, removals, or changes to
  71  * the list since the iterator was created.  Element-changing
  72  * operations on iterators themselves ({@code remove}, {@code set}, and
  73  * {@code add}) are not supported. These methods throw
  74  * {@code UnsupportedOperationException}.




  34 
  35 package java.util.concurrent;
  36 
  37 import java.lang.invoke.VarHandle;
  38 import java.lang.reflect.Field;
  39 import java.util.Arrays;
  40 import java.util.Collection;
  41 import java.util.Comparator;
  42 import java.util.ConcurrentModificationException;
  43 import java.util.Iterator;
  44 import java.util.List;
  45 import java.util.ListIterator;
  46 import java.util.NoSuchElementException;
  47 import java.util.Objects;
  48 import java.util.RandomAccess;
  49 import java.util.Spliterator;
  50 import java.util.Spliterators;
  51 import java.util.function.Consumer;
  52 import java.util.function.Predicate;
  53 import java.util.function.UnaryOperator;
  54 import jdk.internal.access.SharedSecrets;
  55 
  56 /**
  57  * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
  58  * operations ({@code add}, {@code set}, and so on) are implemented by
  59  * making a fresh copy of the underlying array.
  60  *
  61  * <p>This is ordinarily too costly, but may be <em>more</em> efficient
  62  * than alternatives when traversal operations vastly outnumber
  63  * mutations, and is useful when you cannot or don't want to
  64  * synchronize traversals, yet need to preclude interference among
  65  * concurrent threads.  The "snapshot" style iterator method uses a
  66  * reference to the state of the array at the point that the iterator
  67  * was created. This array never changes during the lifetime of the
  68  * iterator, so interference is impossible and the iterator is
  69  * guaranteed not to throw {@code ConcurrentModificationException}.
  70  * The iterator will not reflect additions, removals, or changes to
  71  * the list since the iterator was created.  Element-changing
  72  * operations on iterators themselves ({@code remove}, {@code set}, and
  73  * {@code add}) are not supported. These methods throw
  74  * {@code UnsupportedOperationException}.


< prev index next >