[concurrency-interest] Question about comment 'Not quite a no-op; ensures volatile write semantics' in CopyOnWriteArrayList

Dimitris Andreou jim.andreou at gmail.com
Mon Feb 15 15:12:16 EST 2010


Hi,

I guess it is meant to introduce a happens-before relationships with
subsequent calls to this:

    public E get(int index) {
        return (E)(getArray()[index]);
    }

Because if a set() is invoked to the array (even a no-op), and then a
subsequent get() from another thread, it must be guaranteed (as documented)
that the second thread will see whatever writes the first thread did *prior*
to calling set(). Since get() involves a volatile read (of the array field),
a volatile write on set() (on the array field) is sufficient.

Dimitris

2010/2/15 Peter Oleski <oleski at web.de>

> Hello
>
> The source code of the CopyOnWriteArrayList has in the set method in line
> 399
> a very interesting comment which is:
> "// Not quite a no-op; ensures volatile write semantics"
> This comment is part of an else case which sets the array although nothing
> got changed.
> The central data structure is volatile and defined as:
>
> private volatile transient Object[] array;
>
> Why is it necessary to set the array when nothing has changed?
>
> The Method addAllAbsent in line 717 only calls setArray if something got
> changed.
> Can somebody explain this or is there an explanation already somewhere
> available which I didnt't find?
> I would really like to understand this.
>
>
> Greetings
> Peter Oleski
>
> As a quick reference this is the source of the whole set method.
>
>   /**
>     * Replaces the element at the specified position in this list with the
>     * specified element.
>     *
>     * @throws IndexOutOfBoundsException {@inheritDoc}
>     */
>    public E set(int index, E element) {
>        final ReentrantLock lock = this.lock;
>        lock.lock();
>        try {
>            Object[] elements = getArray();
>            E oldValue = get(elements, index);
>
>            if (oldValue != element) {
>                int len = elements.length;
>                Object[] newElements = Arrays.copyOf(elements, len);
>                newElements[index] = element;
>                setArray(newElements);
>            } else {
>                // Not quite a no-op; ensures volatile write semantics
>                setArray(elements);
>            }
>            return oldValue;
>        } finally {
>            lock.unlock();
>        }
>    }
> ___________________________________________________________
> GRATIS für alle WEB.DE-Nutzer: Die maxdome Movie-FLAT!
> Jetzt freischalten unter http://movieflat.web.de
>
> _______________________________________________
> 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/20100215/7a42b7f8/attachment.html>


More information about the Concurrency-interest mailing list