CopyOnWriteArrayList Collection.shuffle
Zelva Lia
jolyjdia at gmail.com
Wed Aug 24 22:46:42 UTC 2022
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
ср, 24 авг. 2022 г. в 23:26, Jason Mehrens <jason_mehrens at hotmail.com>:
> CopyOnWriteArrayList implements an efficient List::replaceAll so you could
> do something like:
>
> ====
> public static void main(String[] args) {
> List<String> cowl = new CopyOnWriteArrayList<>(new
> String[]{"1","2","3"});
> List<String> copy = Arrays.asList(cowl.toArray(new String[0]));
> Collections.shuffle(copy);
> Iterator<String> it = copy.iterator();
> try {
> cowl.replaceAll(e -> it.next());
> } catch (NoSuchElementException nsee) {
> throw new ConcurrentModificationException();
> }
>
> if (it.hasNext()) {
> throw new ConcurrentModificationException();
> }
>
> System.out.println(cowl);
> }
> ====
>
> 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.
>
> Jason
>
> ________________________________________
> From: core-libs-dev <core-libs-dev-retn at openjdk.org> on behalf of Zelva
> Lia <jolyjdia at gmail.com>
> Sent: Friday, August 19, 2022 5:49 AM
> To: core-libs-dev at openjdk.org
> Subject: CopyOnWriteArrayList Collection.shuffle
>
> 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
>
> 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)
>
>
> COWCollectionsShuffle 0,008 ops/ms
> COWListRandomSort 1,089 ops/ms
> syncListCollectionsShuffle 0,950 ops/ms
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20220825/b6b7ce5d/attachment.htm>
More information about the core-libs-dev
mailing list