8 |
|
import java.util.*; |
9 |
|
|
10 |
|
/** |
11 |
< |
* A Random number generator with the same properties as {@link |
11 |
> |
* A random number generator with the same properties as class {@link |
12 |
|
* Random} but isolated to the current Thread. Like the global |
13 |
< |
* generator used by {@link java.lang.Math}, a ThreadLocalRandom is |
14 |
< |
* initialized with an internally generated seed that may not |
15 |
< |
* otherwise be modified. When applicable, use of ThreadLocalRandom |
16 |
< |
* rather than shared Random objects in concurrent programs will |
17 |
< |
* typically encounter less overhead and contention. The most common |
18 |
< |
* usage form is: <code>ThreadLocalRandom.current().nextX()</code>. |
13 |
> |
* generator used by the {@link java.lang.Math} class, a |
14 |
> |
* ThreadLocalRandom is initialized with an internally generated seed |
15 |
> |
* that may not otherwise be modified. When applicable, use of |
16 |
> |
* ThreadLocalRandom rather than shared Random objects in concurrent |
17 |
> |
* programs will typically encounter much less overhead and |
18 |
> |
* contention. ThreadLocalRandoms are particularly appropriate when |
19 |
> |
* multiple tasks (for example, each a {@link ForkJoinTask}), use |
20 |
> |
* random numbers in parallel in thread pools. |
21 |
> |
* |
22 |
> |
* <p>Usages of this class should typically be of the form: |
23 |
> |
* <code>ThreadLocalRandom.current().nextX(...)</code> (where |
24 |
> |
* <code>X</code> is <code>Int</code>, <code>Long</code>, etc). |
25 |
> |
* When all usages are of this form, it is never possible to |
26 |
> |
* accidently share ThreadLocalRandoms across multiple threads. |
27 |
|
* |
28 |
|
* <p>This class also provides additional commonly used bounded random |
29 |
|
* generation methods. |
43 |
|
* Initialization flag to permit the first and only allowed call |
44 |
|
* to setSeed (inside Random constructor) to succeed. We can't |
45 |
|
* allow others since it would cause setting seed in one part of a |
46 |
< |
* program to inintentionally impact other usages by the thread. |
46 |
> |
* program to unintentionally impact other usages by the thread. |
47 |
|
*/ |
48 |
|
boolean initialized; |
49 |
|
|
52 |
|
// each other. |
53 |
|
private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7; |
54 |
|
|
55 |
+ |
/** |
56 |
+ |
* The actual ThreadLocal |
57 |
+ |
*/ |
58 |
|
private static final ThreadLocal<ThreadLocalRandom> localRandom = |
59 |
|
new ThreadLocal<ThreadLocalRandom>() { |
60 |
|
protected ThreadLocalRandom initialValue() { |
62 |
|
} |
63 |
|
}; |
64 |
|
|
65 |
< |
ThreadLocalRandom() { // construct only in localRandom.initialValue |
66 |
< |
super(); // super constructor calls setSeed |
65 |
> |
|
66 |
> |
/** |
67 |
> |
* Constructor called only by localRandom.initialValue. |
68 |
> |
* We rely on the fact that the superclass no-arg constructor |
69 |
> |
* invokes setSeed exactly once to initialize. |
70 |
> |
*/ |
71 |
> |
ThreadLocalRandom() { |
72 |
> |
super(); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
122 |
|
public long nextLong(long n) { |
123 |
|
if (n <= 0) |
124 |
|
throw new IllegalArgumentException("n must be positive"); |
125 |
+ |
// Divide n by two until small enough for nextInt. On each |
126 |
+ |
// iteration (at most 31 of them but usually much less), |
127 |
+ |
// randomly choose both whether to include high bit in result |
128 |
+ |
// (offset) and whether to continue with the lower vs upper |
129 |
+ |
// half (which makes a difference only if odd). |
130 |
|
long offset = 0; |
131 |
< |
while (n >= Integer.MAX_VALUE) { // randomly pick half range |
132 |
< |
int bits = next(2); // 2nd bit for odd vs even split |
131 |
> |
while (n >= Integer.MAX_VALUE) { |
132 |
> |
int bits = next(2); |
133 |
|
long half = n >>> 1; |
134 |
|
long nextn = ((bits & 2) == 0)? half : n - half; |
135 |
|
if ((bits & 1) == 0) |