[concurrency-interest] Should CopyOnWriteArrayList implement RandomAccess?

Nitsan Wakart nitsanw at yahoo.com
Wed Jun 26 06:55:32 EDT 2013

Given the the javadoc for RandomAccess suggests that:
"As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++) list.get(i);

runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); ) i.next();"
But the above is not true for CopyOnWriteArrayList where the iterator performs better as the volatile read of the array is avoided on each entry and where the first loop is incorrect in any case(can lead to index out of bounds).
Is that a bug or am I missing something here?

More information about the Concurrency-interest mailing list