RFR: 8340572: ConcurrentModificationException when sorting ArrayList sublists

Attila Szegedi attila at openjdk.org
Mon Sep 30 19:46:34 UTC 2024


On Sun, 29 Sep 2024 21:50:21 GMT, David Holmes <dholmes at openjdk.org> wrote:

>> Fixes a regression with #17818 where `ArrayList.subList(…).sort()` started incrementing `ArrayList.modCount` resulting in some cases throwing a `ConcurrentModificationException` where none was thrown before.
>> 
>> This change keeps the optimization from #17818 but restores the behavior where only sorting the `ArrayList` changes the mod count, but sorting its sublists does not.
>
> Just an observation, but sorting is not defined as a "structural modification" but obviously would interfere with an active iterator. So the docs may need updating to include this aspect.

@dholmes-ora and @dfuch both your observations are quite valid and I agree with them. If you look at the [JBS issue](https://bugs.openjdk.org/browse/JDK-8340572), we discussed this topic there. 

Clarifying the collections' behavior with regard to when to throw a CME, and hopefully making that behavior be consistent would be a welcome enhancement. As things stand, the scope of this fix is just ensuring that the behavior of ArrayList reverts back to what it was prior to my optimization, since the change triggered a test failure in some Google test suite. All the while recognizing that the behavior was not particularly consistent to begin with.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/21250#issuecomment-2384017097


More information about the core-libs-dev mailing list