6470700: Math.random() / Math.initRNG() uses "double checked locking"
Replace class Random variable with a static final holder class Reviewed-by: alanb, mduigou, chegar
This commit is contained in:
parent
3cd2e9e099
commit
ff7c51cd18
@ -698,11 +698,8 @@ public final class Math {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Random randomNumberGenerator;
|
private static final class RandomNumberGeneratorHolder {
|
||||||
|
static final Random randomNumberGenerator = new Random();
|
||||||
private static synchronized Random initRNG() {
|
|
||||||
Random rnd = randomNumberGenerator;
|
|
||||||
return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -729,9 +726,7 @@ public final class Math {
|
|||||||
* @see Random#nextDouble()
|
* @see Random#nextDouble()
|
||||||
*/
|
*/
|
||||||
public static double random() {
|
public static double random() {
|
||||||
Random rnd = randomNumberGenerator;
|
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
|
||||||
if (rnd == null) rnd = initRNG();
|
|
||||||
return rnd.nextDouble();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -678,11 +678,8 @@ public final class StrictMath {
|
|||||||
return Math.round(a);
|
return Math.round(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Random randomNumberGenerator;
|
private static final class RandomNumberGeneratorHolder {
|
||||||
|
static final Random randomNumberGenerator = new Random();
|
||||||
private static synchronized Random initRNG() {
|
|
||||||
Random rnd = randomNumberGenerator;
|
|
||||||
return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -709,9 +706,7 @@ public final class StrictMath {
|
|||||||
* @see Random#nextDouble()
|
* @see Random#nextDouble()
|
||||||
*/
|
*/
|
||||||
public static double random() {
|
public static double random() {
|
||||||
Random rnd = randomNumberGenerator;
|
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
|
||||||
if (rnd == null) rnd = initRNG();
|
|
||||||
return rnd.nextDouble();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user