RFR: 8294693: Add Collections.shuffle overload that accepts RandomGenerator interface

Stuart Marks smarks at openjdk.org
Mon Oct 3 21:49:48 UTC 2022


On Sat, 1 Oct 2022 08:06:44 GMT, Tagir F. Valeev <tvaleev at openjdk.org> wrote:

> Java 17 added RandomGenerator interface. However, existing method Collections.shuffle accepts old java.util.Random class. While since Java 19, it's possible to use Random.from(RandomGenerator) wrapper, it would be more convenient to provide direct overload shuffle(List<?> list, RandomGenerator rnd).
> 
> As we are here, it would also be nice to get rid of Collections.r static random generator which is used by default, and use ThreadLocalRandom instead. This will reduce the contention if shuffle() without explicit random generator is called from different threads concurrently.

See my comments in [JDK-8218282](https://bugs.openjdk.org/browse/JDK-8218282). While updating the one-arg Collections::shuffle to use ThreadLocalRandom seems obvious, it's not clear to me that we actually want to do that.

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

PR: https://git.openjdk.org/jdk/pull/10520


More information about the core-libs-dev mailing list