--- jsr166/src/main/java/util/SplittableRandom.java 2016/01/02 02:27:03 1.30 +++ jsr166/src/main/java/util/SplittableRandom.java 2016/12/20 22:08:05 1.38 @@ -219,30 +219,33 @@ public final class SplittableRandom { return seed += gamma; } + // IllegalArgumentException messages + static final String BAD_BOUND = "bound must be positive"; + static final String BAD_RANGE = "bound must be greater than origin"; + static final String BAD_SIZE = "size must be non-negative"; + /** * The seed generator for default constructors. */ - private static final AtomicLong defaultGen = new AtomicLong(initialSeed()); - - private static long initialSeed() { - java.security.PrivilegedAction action = - () -> Boolean.getBoolean("java.util.secureRandomSeed"); - if (java.security.AccessController.doPrivileged(action)) { + private static final AtomicLong defaultGen + = new AtomicLong(mix64(System.currentTimeMillis()) ^ + mix64(System.nanoTime())); + + // at end of to survive static initialization circularity + static { + if (java.security.AccessController.doPrivileged( + new java.security.PrivilegedAction() { + public Boolean run() { + return Boolean.getBoolean("java.util.secureRandomSeed"); + }})) { byte[] seedBytes = java.security.SecureRandom.getSeed(8); long s = (long)seedBytes[0] & 0xffL; for (int i = 1; i < 8; ++i) s = (s << 8) | ((long)seedBytes[i] & 0xffL); - return s; + defaultGen.set(s); } - return (mix64(System.currentTimeMillis()) ^ - mix64(System.nanoTime())); } - // IllegalArgumentException messages - static final String BAD_BOUND = "bound must be positive"; - static final String BAD_RANGE = "bound must be greater than origin"; - static final String BAD_SIZE = "size must be non-negative"; - /* * Internal versions of nextX methods used by streams, as well as * the public nextX(origin, bound) methods. These exist mainly to @@ -372,7 +375,7 @@ public final class SplittableRandom { * may, and typically does, vary across program invocations. */ public SplittableRandom() { // emulate defaultGen.split() - long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA); + long s = defaultGen.getAndAdd(GOLDEN_GAMMA << 1); this.seed = mix64(s); this.gamma = mixGamma(s + GOLDEN_GAMMA); }