9 |
|
import java.util.Random; |
10 |
|
|
11 |
|
/** |
12 |
< |
* A random number generator with the same properties as class {@link |
13 |
< |
* Random} but isolated to the current Thread. Like the global |
14 |
< |
* generator used by the {@link java.lang.Math} class, a |
15 |
< |
* ThreadLocalRandom is initialized with an internally generated seed |
16 |
< |
* that may not otherwise be modified. When applicable, use of |
17 |
< |
* ThreadLocalRandom rather than shared Random objects in concurrent |
18 |
< |
* programs will typically encounter much less overhead and |
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. |
12 |
> |
* A random number generator isolated to the current thread. Like the |
13 |
> |
* global {@link java.util.Random} generator used by the {@link |
14 |
> |
* java.lang.Math} class, a {@code ThreadLocalRandom} is initialized |
15 |
> |
* with an internally generated seed that may not otherwise be |
16 |
> |
* modified. When applicable, use of {@code ThreadLocalRandom} rather |
17 |
> |
* than shared {@code Random} objects in concurrent programs will |
18 |
> |
* typically encounter much less overhead and contention. Use of |
19 |
> |
* {@code ThreadLocalRandom} is particularly appropriate when multiple |
20 |
> |
* tasks (for example, each a {@link ForkJoinTask}) use random numbers |
21 |
> |
* in parallel in thread pools. |
22 |
|
* |
23 |
|
* <p>Usages of this class should typically be of the form: |
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. |
27 |
> |
* accidently share a {@code ThreadLocalRandom} across multiple threads. |
28 |
|
* |
29 |
|
* <p>This class also provides additional commonly used bounded random |
30 |
|
* generation methods. |
44 |
|
private long rnd; |
45 |
|
|
46 |
|
/** |
47 |
< |
* Initialization flag to permit the first and only allowed call |
48 |
< |
* to setSeed (inside Random constructor) to succeed. We can't |
49 |
< |
* allow others since it would cause setting seed in one part of a |
50 |
< |
* program to unintentionally impact other usages by the thread. |
47 |
> |
* Initialization flag to permit calls to setSeed to succeed only |
48 |
> |
* while executing the Random constructor. We can't allow others |
49 |
> |
* since it would cause setting seed in one part of a program to |
50 |
> |
* unintentionally impact other usages by the thread. |
51 |
|
*/ |
52 |
|
boolean initialized; |
53 |
|
|
69 |
|
|
70 |
|
/** |
71 |
|
* Constructor called only by localRandom.initialValue. |
72 |
– |
* We rely on the fact that the superclass no-arg constructor |
73 |
– |
* invokes setSeed exactly once to initialize. |
72 |
|
*/ |
73 |
|
ThreadLocalRandom() { |
74 |
|
super(); |
75 |
+ |
initialized = true; |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
< |
* Returns the current Thread's ThreadLocalRandom. |
79 |
> |
* Returns the current thread's {@code ThreadLocalRandom}. |
80 |
|
* |
81 |
< |
* @return the current Thread's ThreadLocalRandom |
81 |
> |
* @return the current thread's {@code ThreadLocalRandom} |
82 |
|
*/ |
83 |
|
public static ThreadLocalRandom current() { |
84 |
|
return localRandom.get(); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
88 |
< |
* Throws UnsupportedOperationException. Setting seeds in this |
89 |
< |
* generator is unsupported. |
88 |
> |
* Throws {@code UnsupportedOperationException}. Setting seeds in |
89 |
> |
* this generator is not supported. |
90 |
|
* |
91 |
|
* @throws UnsupportedOperationException always |
92 |
|
*/ |
93 |
|
public void setSeed(long seed) { |
94 |
|
if (initialized) |
95 |
|
throw new UnsupportedOperationException(); |
97 |
– |
initialized = true; |
96 |
|
rnd = (seed ^ multiplier) & mask; |
97 |
|
} |
98 |
|
|