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

Comparing jsr166/src/jsr166y/ThreadLocalRandom.java (file contents):
Revision 1.2 by dl, Mon Jan 12 20:46:29 2009 UTC vs.
Revision 1.10 by jsr166, Sat Jul 25 00:34:00 2009 UTC

# Line 5 | Line 5
5   */
6  
7   package jsr166y;
8 < import java.util.*;
8 >
9 > import java.util.Random;
10  
11   /**
12   * A random number generator with the same properties as class {@link
# Line 18 | Line 19 | import java.util.*;
19   * contention.  ThreadLocalRandoms are particularly appropriate when
20   * multiple tasks (for example, each a {@link ForkJoinTask}), use
21   * random numbers in parallel in thread pools.
22 < *
22 > *
23   * <p>Usages of this class should typically be of the form:
24 < * <code>ThreadLocalRandom.current().nextX(...)</code> (where
25 < * <code>X</code> is <code>Int</code>, <code>Long</code>, etc).
24 > * {@code ThreadLocalRandom.current().nextX(...)} (where
25 > * {@code X} is {@code Int}, {@code Long}, etc).
26   * When all usages are of this form, it is never possible to
27   * accidently share ThreadLocalRandoms across multiple threads.
28   *
29   * <p>This class also provides additional commonly used bounded random
30   * generation methods.
31 + *
32 + * @since 1.7
33 + * @author Doug Lea
34   */
35   public class ThreadLocalRandom extends Random {
36      // same constants as Random, but must be redeclared because private
# Line 35 | Line 39 | public class ThreadLocalRandom extends R
39      private final static long mask = (1L << 48) - 1;
40  
41      /**
42 <     * The random seed. We can't use super.seed
42 >     * The random seed. We can't use super.seed.
43       */
44 <    private long rnd;
44 >    private long rnd;
45  
46      /**
47       * Initialization flag to permit the first and only allowed call
# Line 65 | Line 69 | public class ThreadLocalRandom extends R
69  
70      /**
71       * Constructor called only by localRandom.initialValue.
72 <     * We rely on the fact that the superclass no-arg constructor
72 >     * We rely on the fact that the superclass no-arg constructor
73       * invokes setSeed exactly once to initialize.
74       */
75      ThreadLocalRandom() {
# Line 73 | Line 77 | public class ThreadLocalRandom extends R
77      }
78  
79      /**
80 <     * Returns the current Thread's ThreadLocalRandom
80 >     * Returns the current Thread's ThreadLocalRandom.
81 >     *
82       * @return the current Thread's ThreadLocalRandom
83       */
84      public static ThreadLocalRandom current() {
# Line 83 | Line 88 | public class ThreadLocalRandom extends R
88      /**
89       * Throws UnsupportedOperationException. Setting seeds in this
90       * generator is unsupported.
91 <     * @throw UnsupportedOperationException always
91 >     *
92 >     * @throws UnsupportedOperationException always
93       */
94 <    public void setSeed(long seed) {
94 >    public void setSeed(long seed) {
95          if (initialized)
96              throw new UnsupportedOperationException();
97          initialized = true;
# Line 93 | Line 99 | public class ThreadLocalRandom extends R
99      }
100  
101      protected int next(int bits) {
102 <        return (int)((rnd = (rnd * multiplier + addend) & mask) >>> (48-bits));
102 >        rnd = (rnd * multiplier + addend) & mask;
103 >        return (int) (rnd >>> (48-bits));
104      }
105  
106      /**
107       * Returns a pseudorandom, uniformly distributed value between the
108       * given least value (inclusive) and bound (exclusive).
109 +     *
110       * @param least the least value returned
111       * @param bound the upper bound (exclusive)
112       * @throws IllegalArgumentException if least greater than or equal
# Line 113 | Line 121 | public class ThreadLocalRandom extends R
121  
122      /**
123       * Returns a pseudorandom, uniformly distributed value
124 <     * between 0 (inclusive) and the specified value (exclusive)
124 >     * between 0 (inclusive) and the specified value (exclusive).
125 >     *
126       * @param n the bound on the random number to be returned.  Must be
127       *        positive.
128       * @return the next value
# Line 126 | Line 135 | public class ThreadLocalRandom extends R
135          // iteration (at most 31 of them but usually much less),
136          // randomly choose both whether to include high bit in result
137          // (offset) and whether to continue with the lower vs upper
138 <        // half (which makes a difference only if odd).
138 >        // half (which makes a difference only if odd).
139          long offset = 0;
140          while (n >= Integer.MAX_VALUE) {
141 <            int bits = next(2);
141 >            int bits = next(2);
142              long half = n >>> 1;
143 <            long nextn = ((bits & 2) == 0)? half : n - half;
143 >            long nextn = ((bits & 2) == 0) ? half : n - half;
144              if ((bits & 1) == 0)
145                  offset += n - nextn;
146              n = nextn;
147          }
148 <        return offset + nextInt((int)n);
148 >        return offset + nextInt((int) n);
149      }
150  
151      /**
152       * Returns a pseudorandom, uniformly distributed value between the
153       * given least value (inclusive) and bound (exclusive).
154 +     *
155       * @param least the least value returned
156       * @param bound the upper bound (exclusive)
157       * @return the next value
# Line 156 | Line 166 | public class ThreadLocalRandom extends R
166  
167      /**
168       * Returns a pseudorandom, uniformly distributed {@code double} value
169 <     * between 0 (inclusive) and the specified value (exclusive)
169 >     * between 0 (inclusive) and the specified value (exclusive).
170 >     *
171       * @param n the bound on the random number to be returned.  Must be
172       *        positive.
173       * @return the next value
# Line 171 | Line 182 | public class ThreadLocalRandom extends R
182      /**
183       * Returns a pseudorandom, uniformly distributed value between the
184       * given least value (inclusive) and bound (exclusive).
185 +     *
186       * @param least the least value returned
187       * @param bound the upper bound (exclusive)
188       * @return the next value
# Line 183 | Line 195 | public class ThreadLocalRandom extends R
195          return nextDouble() * (bound - least) + least;
196      }
197  
198 < }
198 >    private static final long serialVersionUID = -5851777807851030925L;
199 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines