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

Peter Oleski oleski at web.de
Mon Feb 15 14:33:58 EST 2010


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1742 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20100215/3c3bd1a3/attachment.bin>


More information about the Concurrency-interest mailing list