提交 0a5483f4 编写于 作者: M martin

6959259: Minor improvements to static Random field caching

Summary: Cache fields in locals; small javadoc clarifications
Reviewed-by: emcmanus, dholmes, forax, dl
上级 84574c3d
...@@ -681,9 +681,9 @@ public final class Math { ...@@ -681,9 +681,9 @@ public final class Math {
private static Random randomNumberGenerator; private static Random randomNumberGenerator;
private static synchronized void initRNG() { private static synchronized Random initRNG() {
if (randomNumberGenerator == null) Random rnd = randomNumberGenerator;
randomNumberGenerator = new Random(); return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
} }
/** /**
...@@ -694,9 +694,11 @@ public final class Math { ...@@ -694,9 +694,11 @@ public final class Math {
* *
* <p>When this method is first called, it creates a single new * <p>When this method is first called, it creates a single new
* pseudorandom-number generator, exactly as if by the expression * pseudorandom-number generator, exactly as if by the expression
* <blockquote>{@code new java.util.Random}</blockquote> This *
* new pseudorandom-number generator is used thereafter for all * <blockquote>{@code new java.util.Random()}</blockquote>
* calls to this method and is used nowhere else. *
* This new pseudorandom-number generator is used thereafter for
* all calls to this method and is used nowhere else.
* *
* <p>This method is properly synchronized to allow correct use by * <p>This method is properly synchronized to allow correct use by
* more than one thread. However, if many threads need to generate * more than one thread. However, if many threads need to generate
...@@ -705,11 +707,12 @@ public final class Math { ...@@ -705,11 +707,12 @@ public final class Math {
* *
* @return a pseudorandom {@code double} greater than or equal * @return a pseudorandom {@code double} greater than or equal
* to {@code 0.0} and less than {@code 1.0}. * to {@code 0.0} and less than {@code 1.0}.
* @see java.util.Random#nextDouble() * @see Random#nextDouble()
*/ */
public static double random() { public static double random() {
if (randomNumberGenerator == null) initRNG(); Random rnd = randomNumberGenerator;
return randomNumberGenerator.nextDouble(); if (rnd == null) rnd = initRNG();
return rnd.nextDouble();
} }
/** /**
......
...@@ -667,9 +667,9 @@ public final class StrictMath { ...@@ -667,9 +667,9 @@ public final class StrictMath {
private static Random randomNumberGenerator; private static Random randomNumberGenerator;
private static synchronized void initRNG() { private static synchronized Random initRNG() {
if (randomNumberGenerator == null) Random rnd = randomNumberGenerator;
randomNumberGenerator = new Random(); return (rnd == null) ? (randomNumberGenerator = new Random()) : rnd;
} }
/** /**
...@@ -680,9 +680,11 @@ public final class StrictMath { ...@@ -680,9 +680,11 @@ public final class StrictMath {
* *
* <p>When this method is first called, it creates a single new * <p>When this method is first called, it creates a single new
* pseudorandom-number generator, exactly as if by the expression * pseudorandom-number generator, exactly as if by the expression
* <blockquote>{@code new java.util.Random}</blockquote> This *
* new pseudorandom-number generator is used thereafter for all * <blockquote>{@code new java.util.Random()}</blockquote>
* calls to this method and is used nowhere else. *
* This new pseudorandom-number generator is used thereafter for
* all calls to this method and is used nowhere else.
* *
* <p>This method is properly synchronized to allow correct use by * <p>This method is properly synchronized to allow correct use by
* more than one thread. However, if many threads need to generate * more than one thread. However, if many threads need to generate
...@@ -691,11 +693,12 @@ public final class StrictMath { ...@@ -691,11 +693,12 @@ public final class StrictMath {
* *
* @return a pseudorandom {@code double} greater than or equal * @return a pseudorandom {@code double} greater than or equal
* to {@code 0.0} and less than {@code 1.0}. * to {@code 0.0} and less than {@code 1.0}.
* @see java.util.Random#nextDouble() * @see Random#nextDouble()
*/ */
public static double random() { public static double random() {
if (randomNumberGenerator == null) initRNG(); Random rnd = randomNumberGenerator;
return randomNumberGenerator.nextDouble(); if (rnd == null) rnd = initRNG();
return rnd.nextDouble();
} }
/** /**
......
...@@ -463,10 +463,10 @@ public class Collections { ...@@ -463,10 +463,10 @@ public class Collections {
* its list-iterator does not support the <tt>set</tt> operation. * its list-iterator does not support the <tt>set</tt> operation.
*/ */
public static void shuffle(List<?> list) { public static void shuffle(List<?> list) {
if (r == null) { Random rnd = r;
r = new Random(); if (rnd == null)
} r = rnd = new Random();
shuffle(list, r); shuffle(list, rnd);
} }
private static Random r; private static Random r;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册