Name
CopyOnWriteArrayList<E>
Synopsis
This class is a threadsafe
java.util.List
implementation based on an array.
Any number of read operations may proceed concurrently. All update
methods are synchronized
and make a completely new
copy of the internal array, so this class is best suited to
applications in which reads greatly outnumber updates. The
Iterator
of a
CopyOnWriteArrayList
operates on the copy of the
array that was current when the iterator( )
method
was called: it does not see any updates that occur after the call to
iterator( )
and is guaranteed never to throw
ConcurrentModificationException
. Update methods of
the Iterator
and ListIterator
interfaces are not supported and throw
UnsupportedOperationException
.
CopyOnWriteArrayList
defines a few useful methods
beyond those specified by the List
interface.
addIfAbsent( )
atomically adds an element to the list, but only if the list does not
already contain that element. addAllAbsent( )
adds
all elements of a collection that are not already in the list. Two
new indexOf( )
and lastIndexOf( )
methods are defined that
specify a starting index for the search. These provide a convenient
alternative to using a subList( )
view when
searching for repeated matches in a list.
Figure 16-78. java.util.concurrent.CopyOnWriteArrayList<E>
public class CopyOnWriteArrayList<E> implements java.util.List<E>, java.util.RandomAccess, Cloneable, ...
Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.