Removing a layer of synchronization on Math.random

David Holmes David.Holmes at oracle.com
Tue Jun 8 02:46:51 UTC 2010


Martin,

CR 6959259 filed.

 > Then I noticed that StrictMath.random duplicates
 > the code from Math.random, so I updated
 > StrictMath.random as well.  It would be more
 > maintainable to have StrictMath.random
 > simply call Math.random; I can do that,
 > but other StrictMath methods do no such delegation.

I think that violates the current spec for each class as the RNG can not 
be shared.

David

Martin Buchholz said the following on 06/08/10 05:58:
> On Sun, Jun 6, 2010 at 02:59, Rémi Forax <forax at univ-mlv.fr> wrote:
> 
>> I think initRG can be written like that:
>>
>>  private static synchronized Random initRNG() {
>>    Random randomNumberGenerator = this.randomNumberGenerator;
>>    return (randomNumberGenerator == null)? this.randomNumberGenerator = new
>> Random() : randomNumberGenerator;
>>  }
> 
> Alright, I incorporated this small improvement.
> 
> Then I noticed that StrictMath.random duplicates
> the code from Math.random, so I updated
> StrictMath.random as well.  It would be more
> maintainable to have StrictMath.random
> simply call Math.random; I can do that,
> but other StrictMath methods do no such delegation.
> 
> Then I made a small improvement to another
> static Random field in Collections.java.
> I didn't use double-checked-locking, as with
> Math.random, but at least I removed the
> theoretical never-observed weakness of
> the second read of "r" being null.
> 
> I regenerated the webrev.
> 
> I still need a bugid.
> 
> Martin
> 
>> Rémi



More information about the core-libs-dev mailing list