< prev index next >

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

Print this page
rev 12533 : 8174109: Better queuing priorities
Reviewed-by: smarks


  33  */
  34 
  35 package java.util.concurrent;
  36 import java.util.AbstractList;
  37 import java.util.Arrays;
  38 import java.util.Collection;
  39 import java.util.Comparator;
  40 import java.util.ConcurrentModificationException;
  41 import java.util.Iterator;
  42 import java.util.List;
  43 import java.util.ListIterator;
  44 import java.util.NoSuchElementException;
  45 import java.util.Objects;
  46 import java.util.RandomAccess;
  47 import java.util.Spliterator;
  48 import java.util.Spliterators;
  49 import java.util.concurrent.locks.ReentrantLock;
  50 import java.util.function.Consumer;
  51 import java.util.function.Predicate;
  52 import java.util.function.UnaryOperator;

  53 
  54 /**
  55  * A thread-safe variant of {@link java.util.ArrayList} in which all mutative
  56  * operations ({@code add}, {@code set}, and so on) are implemented by
  57  * making a fresh copy of the underlying array.
  58  *
  59  * <p>This is ordinarily too costly, but may be <em>more</em> efficient
  60  * than alternatives when traversal operations vastly outnumber
  61  * mutations, and is useful when you cannot or don't want to
  62  * synchronize traversals, yet need to preclude interference among
  63  * concurrent threads.  The "snapshot" style iterator method uses a
  64  * reference to the state of the array at the point that the iterator
  65  * was created. This array never changes during the lifetime of the
  66  * iterator, so interference is impossible and the iterator is
  67  * guaranteed not to throw {@code ConcurrentModificationException}.
  68  * The iterator will not reflect additions, removals, or changes to
  69  * the list since the iterator was created.  Element-changing
  70  * operations on iterators themselves ({@code remove}, {@code set}, and
  71  * {@code add}) are not supported. These methods throw
  72  * {@code UnsupportedOperationException}.


 972             s.writeObject(element);
 973     }
 974 
 975     /**
 976      * Reconstitutes this list from a stream (that is, deserializes it).
 977      * @param s the stream
 978      * @throws ClassNotFoundException if the class of a serialized object
 979      *         could not be found
 980      * @throws java.io.IOException if an I/O error occurs
 981      */
 982     private void readObject(java.io.ObjectInputStream s)
 983         throws java.io.IOException, ClassNotFoundException {
 984 
 985         s.defaultReadObject();
 986 
 987         // bind to new lock
 988         resetLock();
 989 
 990         // Read in array length and allocate array
 991         int len = s.readInt();

 992         Object[] elements = new Object[len];
 993 
 994         // Read in all elements in the proper order.
 995         for (int i = 0; i < len; i++)
 996             elements[i] = s.readObject();
 997         setArray(elements);
 998     }
 999 
1000     /**
1001      * Returns a string representation of this list.  The string
1002      * representation consists of the string representations of the list's
1003      * elements in the order they are returned by its iterator, enclosed in
1004      * square brackets ({@code "[]"}).  Adjacent elements are separated by
1005      * the characters {@code ", "} (comma and space).  Elements are
1006      * converted to strings as by {@link String#valueOf(Object)}.
1007      *
1008      * @return a string representation of this list
1009      */
1010     public String toString() {
1011         return Arrays.toString(getArray());




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


 973             s.writeObject(element);
 974     }
 975 
 976     /**
 977      * Reconstitutes this list from a stream (that is, deserializes it).
 978      * @param s the stream
 979      * @throws ClassNotFoundException if the class of a serialized object
 980      *         could not be found
 981      * @throws java.io.IOException if an I/O error occurs
 982      */
 983     private void readObject(java.io.ObjectInputStream s)
 984         throws java.io.IOException, ClassNotFoundException {
 985 
 986         s.defaultReadObject();
 987 
 988         // bind to new lock
 989         resetLock();
 990 
 991         // Read in array length and allocate array
 992         int len = s.readInt();
 993         SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, len);
 994         Object[] elements = new Object[len];
 995 
 996         // Read in all elements in the proper order.
 997         for (int i = 0; i < len; i++)
 998             elements[i] = s.readObject();
 999         setArray(elements);
1000     }
1001 
1002     /**
1003      * Returns a string representation of this list.  The string
1004      * representation consists of the string representations of the list's
1005      * elements in the order they are returned by its iterator, enclosed in
1006      * square brackets ({@code "[]"}).  Adjacent elements are separated by
1007      * the characters {@code ", "} (comma and space).  Elements are
1008      * converted to strings as by {@link String#valueOf(Object)}.
1009      *
1010      * @return a string representation of this list
1011      */
1012     public String toString() {
1013         return Arrays.toString(getArray());


< prev index next >