提交 ad23f9b3 编写于 作者: B bpb

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: NBrian Burkhalter <brian.burkhalter@oracle.com>
上级 bb38ccc4
......@@ -698,11 +698,8 @@ public final class Math {
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 @@ public final class Math {
* @see Random#nextDouble()
*/
public static double random() {
Random rnd = randomNumberGenerator;
if (rnd == null) rnd = initRNG();
return rnd.nextDouble();
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
/**
......
......@@ -678,11 +678,8 @@ public final class StrictMath {
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 @@ public final class StrictMath {
* @see Random#nextDouble()
*/
public static double random() {
Random rnd = randomNumberGenerator;
if (rnd == null) rnd = initRNG();
return rnd.nextDouble();
return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册