Removing a layer of synchronization on Math.random

Martin Buchholz martinrb at google.com
Mon Jun 7 19:58:10 UTC 2010


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