Java Reference
In-Depth Information
that the size of the
Vector
does not change between calling
size
and
get
, as shown in
The risk that the size of the list might change between a call to
size
and the corresponding
call to
get
is also present when we iterate through the elements of a
Vector
as shown in
This iteration idiom relies on a leap of faith that other threads will not modify the
Vector
between the calls to
size
and
get
. In a single-threaded environment, this assumption is
perfectly valid, but when other threads may concurrently modify the
Vector
it can lead to
trouble. Just as with
getLast
, if another thread deletes an element while you are iterating
through the
Vector
and the operations are interleaved unluckily, this iteration idiom throws
ArrayIndexOutOfBoundsException
.
Listing 5.2. Compound Actions on
Vector
Using Client-side Locking.
Listing 5.3. Iteration that may Throw
ArrayIndexOutOfBoundsException
.