# HG changeset patch # User martin # Date 1276210526 25200 # Node ID 11a42d91eb5622be307d5fbbc99c188639001586 # Parent 3f7be5f312ae6bba7bbc0aba3153895630788c7b 6959259: Minor improvements to static Random field caching Summary: Cache fields in locals; small javadoc clarifications Reviewed-by: emcmanus, dholmes, forax, dl diff -r 3f7be5f312ae -r 11a42d91eb56 jdk/src/share/classes/java/lang/Math.java --- a/jdk/src/share/classes/java/lang/Math.java Thu Jun 10 15:54:25 2010 -0700 +++ b/jdk/src/share/classes/java/lang/Math.java Thu Jun 10 15:55:26 2010 -0700 @@ -681,9 +681,9 @@ private static Random randomNumberGenerator; - private static synchronized void initRNG() { - if (randomNumberGenerator == null) - randomNumberGenerator = new Random(); + private static synchronized Random initRNG() { + Random rnd = randomNumberGenerator; + return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd; } /** @@ -694,9 +694,11 @@ * *
When this method is first called, it creates a single new * pseudorandom-number generator, exactly as if by the expression - *
{@code new java.util.Random}This - * new pseudorandom-number generator is used thereafter for all - * calls to this method and is used nowhere else. + * + *
{@code new java.util.Random()}+ * + * This new pseudorandom-number generator is used thereafter for + * all calls to this method and is used nowhere else. * *
This method is properly synchronized to allow correct use by * more than one thread. However, if many threads need to generate @@ -705,11 +707,12 @@ * * @return a pseudorandom {@code double} greater than or equal * to {@code 0.0} and less than {@code 1.0}. - * @see java.util.Random#nextDouble() + * @see Random#nextDouble() */ public static double random() { - if (randomNumberGenerator == null) initRNG(); - return randomNumberGenerator.nextDouble(); + Random rnd = randomNumberGenerator; + if (rnd == null) rnd = initRNG(); + return rnd.nextDouble(); } /** diff -r 3f7be5f312ae -r 11a42d91eb56 jdk/src/share/classes/java/lang/StrictMath.java --- a/jdk/src/share/classes/java/lang/StrictMath.java Thu Jun 10 15:54:25 2010 -0700 +++ b/jdk/src/share/classes/java/lang/StrictMath.java Thu Jun 10 15:55:26 2010 -0700 @@ -667,9 +667,9 @@ private static Random randomNumberGenerator; - private static synchronized void initRNG() { - if (randomNumberGenerator == null) - randomNumberGenerator = new Random(); + private static synchronized Random initRNG() { + Random rnd = randomNumberGenerator; + return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd; } /** @@ -680,9 +680,11 @@ * *
When this method is first called, it creates a single new * pseudorandom-number generator, exactly as if by the expression - *
{@code new java.util.Random}This - * new pseudorandom-number generator is used thereafter for all - * calls to this method and is used nowhere else. + * + *
{@code new java.util.Random()}+ * + * This new pseudorandom-number generator is used thereafter for + * all calls to this method and is used nowhere else. * *
This method is properly synchronized to allow correct use by * more than one thread. However, if many threads need to generate @@ -691,11 +693,12 @@ * * @return a pseudorandom {@code double} greater than or equal * to {@code 0.0} and less than {@code 1.0}. - * @see java.util.Random#nextDouble() + * @see Random#nextDouble() */ public static double random() { - if (randomNumberGenerator == null) initRNG(); - return randomNumberGenerator.nextDouble(); + Random rnd = randomNumberGenerator; + if (rnd == null) rnd = initRNG(); + return rnd.nextDouble(); } /** diff -r 3f7be5f312ae -r 11a42d91eb56 jdk/src/share/classes/java/util/Collections.java --- a/jdk/src/share/classes/java/util/Collections.java Thu Jun 10 15:54:25 2010 -0700 +++ b/jdk/src/share/classes/java/util/Collections.java Thu Jun 10 15:55:26 2010 -0700 @@ -463,10 +463,10 @@ * its list-iterator does not support the set operation. */ public static void shuffle(List> list) { - if (r == null) { - r = new Random(); - } - shuffle(list, r); + Random rnd = r; + if (rnd == null) + r = rnd = new Random(); + shuffle(list, rnd); } private static Random r;