6470700: Math.random() / Math.initRNG() uses "double checked locking"
Summary: Replace class Random variable with a static final holder class
Reviewed-by: alanb, mduigou, chegar
Contributed-by: Brian Burkhalter <brian.burkhalter@oracle.com>
diff --git a/src/share/classes/java/lang/Math.java b/src/share/classes/java/lang/Math.java
index 23bc934..ae83e42 100644
--- a/src/share/classes/java/lang/Math.java
+++ b/src/share/classes/java/lang/Math.java
@@ -698,11 +698,8 @@
             return 0;
     }
 
-    private static Random randomNumberGenerator;
-
-    private static synchronized Random initRNG() {
-        Random rnd = randomNumberGenerator;
-        return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
+    private static final class RandomNumberGeneratorHolder {
+        static final Random randomNumberGenerator = new Random();
     }
 
     /**
@@ -729,9 +726,7 @@
      * @see Random#nextDouble()
      */
     public static double random() {
-        Random rnd = randomNumberGenerator;
-        if (rnd == null) rnd = initRNG();
-        return rnd.nextDouble();
+        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
     }
 
     /**
diff --git a/src/share/classes/java/lang/StrictMath.java b/src/share/classes/java/lang/StrictMath.java
index eb202d5..5233648 100644
--- a/src/share/classes/java/lang/StrictMath.java
+++ b/src/share/classes/java/lang/StrictMath.java
@@ -678,11 +678,8 @@
         return Math.round(a);
     }
 
-    private static Random randomNumberGenerator;
-
-    private static synchronized Random initRNG() {
-        Random rnd = randomNumberGenerator;
-        return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
+    private static final class RandomNumberGeneratorHolder {
+        static final Random randomNumberGenerator = new Random();
     }
 
     /**
@@ -709,9 +706,7 @@
      * @see Random#nextDouble()
      */
     public static double random() {
-        Random rnd = randomNumberGenerator;
-        if (rnd == null) rnd = initRNG();
-        return rnd.nextDouble();
+        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
     }
 
     /**