[concurrency-interest] Should CopyOnWriteArrayList implement RandomAccess?

Stanimir Simeonoff stanimir at riflexo.com
Wed Jun 26 08:06:52 EDT 2013

I can't think of a practical use of loop and COWList.get. If you are to
concurrently modify and read a concurrent structure many algorithm won't
work - sorting, reverse, etc.
Even so the volatile read might not be slower than the iterator use, the
volatile read can be as cheap as L1 cache hit. Still the JIT cannot
aggressively optimized the access to the array.


On Wed, Jun 26, 2013 at 1:55 PM, Nitsan Wakart <nitsanw at yahoo.com> wrote:

> Hi,
> 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?
> Thanks,
> Nitsan
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20130626/b5e96ee3/attachment.html>

More information about the Concurrency-interest mailing list