41 |
|
* |
42 |
|
* <p>All elements are permitted, including <tt>null</tt>. |
43 |
|
* |
44 |
+ |
* <p>Memory consistency effects: As with other concurrent |
45 |
+ |
* collections, actions in a thread prior to placing an object into a |
46 |
+ |
* {@code CopyOnWriteArrayList} |
47 |
+ |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
48 |
+ |
* actions subsequent to the access or removal of that element from |
49 |
+ |
* the {@code CopyOnWriteArrayList} in another thread. |
50 |
+ |
* |
51 |
|
* <p>This class is a member of the |
52 |
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
53 |
|
* Java Collections Framework</a>. |
54 |
|
* |
48 |
– |
* <p> |
49 |
– |
* Memory consistency effects: As with other concurrent collections, state |
50 |
– |
* changes to any object made prior to placing it into a <tt>CopyOnWriteArrayList</tt> |
51 |
– |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
52 |
– |
* that element is accessed via or removed from the <tt>CopyOnWriteArrayList</tt>. |
53 |
– |
* |
55 |
|
* @since 1.5 |
56 |
|
* @author Doug Lea |
57 |
|
* @param <E> the type of elements held in this collection |
351 |
|
lock.lock(); |
352 |
|
try { |
353 |
|
Object[] elements = getArray(); |
353 |
– |
int len = elements.length; |
354 |
|
Object oldValue = elements[index]; |
355 |
|
|
356 |
|
if (oldValue != element) { |
357 |
+ |
int len = elements.length; |
358 |
|
Object[] newElements = Arrays.copyOf(elements, len); |
359 |
|
newElements[index] = element; |
360 |
|
setArray(newElements); |
361 |
+ |
} else { |
362 |
+ |
// Not quite a no-op; ensures volatile write semantics |
363 |
+ |
setArray(elements); |
364 |
|
} |
365 |
|
return (E)oldValue; |
366 |
|
} finally { |
1280 |
|
} |
1281 |
|
|
1282 |
|
// Support for resetting lock while deserializing |
1283 |
< |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
1283 |
> |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
1284 |
|
private static final long lockOffset; |
1285 |
|
static { |
1286 |
|
try { |