ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ThreadLocalRandom.java (file contents):
Revision 1.40 by jsr166, Fri Feb 19 03:39:15 2016 UTC vs.
Revision 1.41 by jsr166, Sat Feb 20 23:24:57 2016 UTC

# Line 96 | Line 96 | public class ThreadLocalRandom extends R
96       * but we provide identical statistical properties.
97       */
98  
99    /** Generates per-thread initialization/probe field */
100    private static final AtomicInteger probeGenerator = new AtomicInteger();
101
102    /**
103     * The next seed for default constructors.
104     */
105    private static final AtomicLong seeder = new AtomicLong(initialSeed());
106
107    private static long initialSeed() {
108        if (java.security.AccessController.doPrivileged(
109            new java.security.PrivilegedAction<Boolean>() {
110                public Boolean run() {
111                    return Boolean.getBoolean("java.util.secureRandomSeed");
112                }})) {
113            byte[] seedBytes = java.security.SecureRandom.getSeed(8);
114            long s = (long)seedBytes[0] & 0xffL;
115            for (int i = 1; i < 8; ++i)
116                s = (s << 8) | ((long)seedBytes[i] & 0xffL);
117            return s;
118        }
119        return (mix64(System.currentTimeMillis()) ^
120                mix64(System.nanoTime()));
121    }
122
123    /**
124     * The seed increment.
125     */
126    private static final long GAMMA = 0x9e3779b97f4a7c15L;
127
128    /**
129     * The increment for generating probe values.
130     */
131    private static final int PROBE_INCREMENT = 0x9e3779b9;
132
133    /**
134     * The increment of seeder per new instance.
135     */
136    private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL;
137
138    // Constants from SplittableRandom
139    private static final double DOUBLE_UNIT = 0x1.0p-53;  // 1.0  / (1L << 53)
140    private static final float  FLOAT_UNIT  = 0x1.0p-24f; // 1.0f / (1 << 24)
141
142    /** Rarely-used holder for the second of a pair of Gaussians */
143    private static final ThreadLocal<Double> nextLocalGaussian =
144        new ThreadLocal<>();
145
99      private static long mix64(long z) {
100          z = (z ^ (z >>> 33)) * 0xff51afd7ed558ccdL;
101          z = (z ^ (z >>> 33)) * 0xc4ceb9fe1a85ec53L;
# Line 165 | Line 118 | public class ThreadLocalRandom extends R
118          initialized = true; // false during super() call
119      }
120  
168    /** The common ThreadLocalRandom */
169    static final ThreadLocalRandom instance = new ThreadLocalRandom();
170
121      /**
122       * Initialize Thread fields for the current thread.  Called only
123       * when Thread.threadLocalRandomProbe is zero, indicating that a
# Line 219 | Line 169 | public class ThreadLocalRandom extends R
169          return (int)(mix64(nextSeed()) >>> (64 - bits));
170      }
171  
222    // IllegalArgumentException messages
223    static final String BAD_BOUND = "bound must be positive";
224    static final String BAD_RANGE = "bound must be greater than origin";
225    static final String BAD_SIZE  = "size must be non-negative";
226
172      /**
173       * The form of nextLong used by LongStream Spliterators.  If
174       * origin is greater than bound, acts as unbounded form of
# Line 1021 | Line 966 | public class ThreadLocalRandom extends R
966          return current();
967      }
968  
969 +    // Static initialization
970 +
971 +    /**
972 +     * The seed increment.
973 +     */
974 +    private static final long GAMMA = 0x9e3779b97f4a7c15L;
975 +
976 +    /**
977 +     * The increment for generating probe values.
978 +     */
979 +    private static final int PROBE_INCREMENT = 0x9e3779b9;
980 +
981 +    /**
982 +     * The increment of seeder per new instance.
983 +     */
984 +    private static final long SEEDER_INCREMENT = 0xbb67ae8584caa73bL;
985 +
986 +    // Constants from SplittableRandom
987 +    private static final double DOUBLE_UNIT = 0x1.0p-53;  // 1.0  / (1L << 53)
988 +    private static final float  FLOAT_UNIT  = 0x1.0p-24f; // 1.0f / (1 << 24)
989 +
990 +    // IllegalArgumentException messages
991 +    static final String BAD_BOUND = "bound must be positive";
992 +    static final String BAD_RANGE = "bound must be greater than origin";
993 +    static final String BAD_SIZE  = "size must be non-negative";
994 +
995      // Unsafe mechanics
996      private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
997      private static final long SEED;
# Line 1038 | Line 1009 | public class ThreadLocalRandom extends R
1009              throw new Error(e);
1010          }
1011      }
1012 +
1013 +    /** Rarely-used holder for the second of a pair of Gaussians */
1014 +    private static final ThreadLocal<Double> nextLocalGaussian =
1015 +        new ThreadLocal<>();
1016 +
1017 +    /** Generates per-thread initialization/probe field */
1018 +    private static final AtomicInteger probeGenerator = new AtomicInteger();
1019 +
1020 +    /** The common ThreadLocalRandom */
1021 +    static final ThreadLocalRandom instance = new ThreadLocalRandom();
1022 +
1023 +    /**
1024 +     * The next seed for default constructors.
1025 +     */
1026 +    private static final AtomicLong seeder
1027 +        = new AtomicLong(mix64(System.currentTimeMillis()) ^
1028 +                         mix64(System.nanoTime()));
1029 +
1030 +    // at end of <clinit> to survive static initialization circularity
1031 +    static {
1032 +        if (java.security.AccessController.doPrivileged(
1033 +            new java.security.PrivilegedAction<Boolean>() {
1034 +                public Boolean run() {
1035 +                    return Boolean.getBoolean("java.util.secureRandomSeed");
1036 +                }})) {
1037 +            byte[] seedBytes = java.security.SecureRandom.getSeed(8);
1038 +            long s = (long)seedBytes[0] & 0xffL;
1039 +            for (int i = 1; i < 8; ++i)
1040 +                s = (s << 8) | ((long)seedBytes[i] & 0xffL);
1041 +            seeder.set(s);
1042 +        }
1043 +    }
1044   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines