How can CopyOnWriteArrayList be thread-safe?

Posted by Shooshpanchick on Stack Overflow See other posts from Stack Overflow or by Shooshpanchick
Published on 2010-06-01T15:01:16Z Indexed on 2010/06/01 15:03 UTC
Read the original article Hit count: 177

I've taken a look into OpenJDK's sources of CopyOnWriteArrayList and it seems that all write operations are protected by the same lock and read operations are not protected at all. As I understand, under JMM all accesses to a variable (both read and write) should be protected by lock or reordering effects may occur.

For example, set(int, E) method contains these lines (under lock):

/* 1 */ int len = elements.length;
/* 2 */ Object[] newElements = Arrays.copyOf(elements, len);
/* 3 */ newElements[index] = element;
/* 4 */ setArray(newElements);

The get(int) method, on the other hand, only does return get(getArray(), index);.

In my understanding of JMM, this means that get may observe the array in an inconsistent state if statements 1-4 are reordered like 1-2(new)-4-2(copyOf)-3.

Do I understand JMM incorrectly or is there any other explanations on why CopyOnWriteArrayList is thread-safe?

© Stack Overflow or respective owner

Related posts about java

Related posts about concurrency