<div dir="auto"><div dir="ltr">Well, yes, this is a solvable problem, albeit with additional copying, but solvable, which cannot be said about other operations (for example, swap), I lead to the fact that there are few specialized methods in COW that can work inside synchronization and cannot be synchronized with by the COWArrayList object itself, because it is synchronized with its private lock</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">ср, 24 авг. 2022 г. в 23:26, Jason Mehrens <<a href="mailto:jason_mehrens@hotmail.com" target="_blank" rel="noreferrer">jason_mehrens@hotmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">CopyOnWriteArrayList implements an efficient List::replaceAll so you could do something like:<br>
<br>
====<br>
public static void main(String[] args) {<br>
List<String> cowl = new CopyOnWriteArrayList<>(new String[]{"1","2","3"});<br>
List<String> copy = Arrays.asList(cowl.toArray(new String[0]));<br>
Collections.shuffle(copy);<br>
Iterator<String> it = copy.iterator(); <br>
try {<br>
cowl.replaceAll(e -> it.next());<br>
} catch (NoSuchElementException nsee) {<br>
throw new ConcurrentModificationException();<br>
}<br>
<br>
if (it.hasNext()) {<br>
throw new ConcurrentModificationException();<br>
}<br>
<br>
System.out.println(cowl);<br>
}<br>
====<br>
<br>
Some of the non-random access branches in Collections.java could be updated to use List::replaceAll over ListIterator next/set. Once Collections.java was using List::replaceAll you could then just wrap CopyOnWriteArrayList in a non-random access list.<br>
<br>
Jason<br>
<br>
________________________________________<br>
From: core-libs-dev <<a href="mailto:core-libs-dev-retn@openjdk.org" target="_blank" rel="noreferrer">core-libs-dev-retn@openjdk.org</a>> on behalf of Zelva Lia <<a href="mailto:jolyjdia@gmail.com" target="_blank" rel="noreferrer">jolyjdia@gmail.com</a>><br>
Sent: Friday, August 19, 2022 5:49 AM<br>
To: <a href="mailto:core-libs-dev@openjdk.org" target="_blank" rel="noreferrer">core-libs-dev@openjdk.org</a><br>
Subject: CopyOnWriteArrayList Collection.shuffle<br>
<br>
Hello, when shuffling the CopyOnWrite list with the standard Collections.shuffle method, performance anomalies occur, due to the fact that it calls the set method, which copies the array each time, a possible solution (crutch) is a random comparator for sorting, so sorting in COW is redefined to its own sub - blocking implementation<br>
<br>
Another problem with Collections.shuffle is that it's not exactly thread safe because it calls the size() method; and then iterates from it, also COW does not support modification inside the iterator (works on snapshots)<br>
<br>
<br>
COWCollectionsShuffle 0,008 ops/ms<br>
COWListRandomSort 1,089 ops/ms<br>
syncListCollectionsShuffle 0,950 ops/ms<br>
<br>
</blockquote></div></div>