RFR: 8279598: Provide adapter from RandomGenerator to Random [v6]

liach duke at openjdk.java.net
Tue Feb 22 23:40:46 UTC 2022


On Sun, 20 Feb 2022 06:39:26 GMT, liach <duke at openjdk.java.net> wrote:

>> Yasser Bazzi has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   remove missed whitespace
>
> src/java.base/share/classes/java/util/Random.java line 95:
> 
>> 93:     private static class RandomWrapper extends Random implements RandomGenerator {
>> 94:         private final RandomGenerator generator;
>> 95:         private final boolean initialized;
> 
> Can we create a private or package-private constructor for `Random` for this subclass' constructor to call? The no-arg constructor has some unnecessary calculations, and the `long` constructor calls `setSeed`. A special `Random` constructor for this subclass allows removing this special logic (by not calling `setSeed` at all) and avoid redundant seed initialization.

More specifically, in Random class, declare

// special constructor for RandomWrapper to call
private Random(Void unused) {
    this.seed = new AtomicLong();
}


So the randomwrapper can be (with `initialized` field removed):

private RandomWrapper(RandomGenerator randomToWrap) {
    super(null);
    this.generator = randomToWrap;
}

@Override
public void setSeed(long seed) {
    throw new UnsupportedOperationException();
}


Currently, the RandomWrapper constructor calls the no-arg super constructor implicitly, which has some overheads for unnecessary calculations.

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

PR: https://git.openjdk.java.net/jdk/pull/7001


More information about the core-libs-dev mailing list