diff --git a/src/share/classes/java/lang/Math.java b/src/share/classes/java/lang/Math.java index 7ce31265d6066f1197cc181b8a2524d781d81a38..455a21f9a4c09fd0554053389e5250087e9439d9 100644 --- a/src/share/classes/java/lang/Math.java +++ b/src/share/classes/java/lang/Math.java @@ -681,9 +681,9 @@ public final class Math { 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 @@ public final class Math { * *

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 @@ public final class Math { * * @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 --git a/src/share/classes/java/lang/StrictMath.java b/src/share/classes/java/lang/StrictMath.java index f2f275b5ca78f4446f1c8fd50b91bf1a76df67e8..916b82ff9d043e30076d0063fe783d7d68d6f3e4 100644 --- a/src/share/classes/java/lang/StrictMath.java +++ b/src/share/classes/java/lang/StrictMath.java @@ -667,9 +667,9 @@ public final class StrictMath { 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 @@ public final class StrictMath { * *

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 @@ public final class StrictMath { * * @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 --git a/src/share/classes/java/util/Collections.java b/src/share/classes/java/util/Collections.java index 8b20182f7450af3fe17f183b6cec9d65d066c3bc..e4329dc0e43d83295acf48d6918aa927aae94af8 100644 --- a/src/share/classes/java/util/Collections.java +++ b/src/share/classes/java/util/Collections.java @@ -463,10 +463,10 @@ public class Collections { * 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;