Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/util/concurrent/ThreadLocalRandom.java
+++ new/src/share/classes/java/util/concurrent/ThreadLocalRandom.java
1 1 /*
2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 3 *
4 4 * This code is free software; you can redistribute it and/or modify it
5 5 * under the terms of the GNU General Public License version 2 only, as
6 6 * published by the Free Software Foundation. Oracle designates this
7 7 * particular file as subject to the "Classpath" exception as provided
8 8 * by Oracle in the LICENSE file that accompanied this code.
9 9 *
10 10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 13 * version 2 for more details (a copy is included in the LICENSE file that
14 14 * accompanied this code).
15 15 *
16 16 * You should have received a copy of the GNU General Public License version
17 17 * 2 along with this work; if not, write to the Free Software Foundation,
18 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 19 *
20 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 21 * or visit www.oracle.com if you need additional information or have any
22 22 * questions.
23 23 */
24 24
25 25 /*
26 26 * This file is available under and governed by the GNU General Public
27 27 * License version 2 only, as published by the Free Software Foundation.
28 28 * However, the following notice accompanied the original version of this
29 29 * file:
30 30 *
31 31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 32 * Expert Group and released to the public domain, as explained at
33 33 * http://creativecommons.org/licenses/publicdomain
34 34 */
35 35
36 36 package java.util.concurrent;
37 37
38 38 import java.util.Random;
39 39
40 40 /**
41 41 * A random number generator isolated to the current thread. Like the
42 42 * global {@link java.util.Random} generator used by the {@link
43 43 * java.lang.Math} class, a {@code ThreadLocalRandom} is initialized
44 44 * with an internally generated seed that may not otherwise be
45 45 * modified. When applicable, use of {@code ThreadLocalRandom} rather
46 46 * than shared {@code Random} objects in concurrent programs will
47 47 * typically encounter much less overhead and contention. Use of
48 48 * {@code ThreadLocalRandom} is particularly appropriate when multiple
49 49 * tasks (for example, each a {@link ForkJoinTask}) use random numbers
50 50 * in parallel in thread pools.
51 51 *
52 52 * <p>Usages of this class should typically be of the form:
53 53 * {@code ThreadLocalRandom.current().nextX(...)} (where
54 54 * {@code X} is {@code Int}, {@code Long}, etc).
55 55 * When all usages are of this form, it is never possible to
↓ open down ↓ |
55 lines elided |
↑ open up ↑ |
56 56 * accidently share a {@code ThreadLocalRandom} across multiple threads.
57 57 *
58 58 * <p>This class also provides additional commonly used bounded random
59 59 * generation methods.
60 60 *
61 61 * @since 1.7
62 62 * @author Doug Lea
63 63 */
64 64 public class ThreadLocalRandom extends Random {
65 65 // same constants as Random, but must be redeclared because private
66 - private final static long multiplier = 0x5DEECE66DL;
67 - private final static long addend = 0xBL;
68 - private final static long mask = (1L << 48) - 1;
66 + private static final long multiplier = 0x5DEECE66DL;
67 + private static final long addend = 0xBL;
68 + private static final long mask = (1L << 48) - 1;
69 69
70 70 /**
71 71 * The random seed. We can't use super.seed.
72 72 */
73 73 private long rnd;
74 74
75 75 /**
76 76 * Initialization flag to permit calls to setSeed to succeed only
77 77 * while executing the Random constructor. We can't allow others
78 78 * since it would cause setting seed in one part of a program to
79 79 * unintentionally impact other usages by the thread.
80 80 */
81 81 boolean initialized;
82 82
83 83 // Padding to help avoid memory contention among seed updates in
84 84 // different TLRs in the common case that they are located near
85 85 // each other.
86 86 private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
87 87
88 88 /**
89 89 * The actual ThreadLocal
90 90 */
91 91 private static final ThreadLocal<ThreadLocalRandom> localRandom =
92 92 new ThreadLocal<ThreadLocalRandom>() {
93 93 protected ThreadLocalRandom initialValue() {
94 94 return new ThreadLocalRandom();
95 95 }
96 96 };
97 97
98 98
99 99 /**
100 100 * Constructor called only by localRandom.initialValue.
101 101 */
102 102 ThreadLocalRandom() {
103 103 super();
104 104 initialized = true;
105 105 }
106 106
107 107 /**
108 108 * Returns the current thread's {@code ThreadLocalRandom}.
109 109 *
110 110 * @return the current thread's {@code ThreadLocalRandom}
111 111 */
112 112 public static ThreadLocalRandom current() {
113 113 return localRandom.get();
114 114 }
115 115
116 116 /**
117 117 * Throws {@code UnsupportedOperationException}. Setting seeds in
118 118 * this generator is not supported.
119 119 *
120 120 * @throws UnsupportedOperationException always
121 121 */
122 122 public void setSeed(long seed) {
123 123 if (initialized)
124 124 throw new UnsupportedOperationException();
125 125 rnd = (seed ^ multiplier) & mask;
126 126 }
127 127
128 128 protected int next(int bits) {
129 129 rnd = (rnd * multiplier + addend) & mask;
130 130 return (int) (rnd >>> (48-bits));
131 131 }
132 132
133 133 /**
134 134 * Returns a pseudorandom, uniformly distributed value between the
135 135 * given least value (inclusive) and bound (exclusive).
136 136 *
137 137 * @param least the least value returned
138 138 * @param bound the upper bound (exclusive)
139 139 * @throws IllegalArgumentException if least greater than or equal
140 140 * to bound
141 141 * @return the next value
142 142 */
143 143 public int nextInt(int least, int bound) {
144 144 if (least >= bound)
145 145 throw new IllegalArgumentException();
146 146 return nextInt(bound - least) + least;
147 147 }
148 148
149 149 /**
150 150 * Returns a pseudorandom, uniformly distributed value
151 151 * between 0 (inclusive) and the specified value (exclusive).
152 152 *
153 153 * @param n the bound on the random number to be returned. Must be
154 154 * positive.
155 155 * @return the next value
156 156 * @throws IllegalArgumentException if n is not positive
157 157 */
158 158 public long nextLong(long n) {
159 159 if (n <= 0)
160 160 throw new IllegalArgumentException("n must be positive");
161 161 // Divide n by two until small enough for nextInt. On each
162 162 // iteration (at most 31 of them but usually much less),
163 163 // randomly choose both whether to include high bit in result
164 164 // (offset) and whether to continue with the lower vs upper
165 165 // half (which makes a difference only if odd).
166 166 long offset = 0;
167 167 while (n >= Integer.MAX_VALUE) {
168 168 int bits = next(2);
169 169 long half = n >>> 1;
170 170 long nextn = ((bits & 2) == 0) ? half : n - half;
171 171 if ((bits & 1) == 0)
172 172 offset += n - nextn;
173 173 n = nextn;
174 174 }
175 175 return offset + nextInt((int) n);
176 176 }
177 177
178 178 /**
179 179 * Returns a pseudorandom, uniformly distributed value between the
180 180 * given least value (inclusive) and bound (exclusive).
181 181 *
182 182 * @param least the least value returned
183 183 * @param bound the upper bound (exclusive)
184 184 * @return the next value
185 185 * @throws IllegalArgumentException if least greater than or equal
186 186 * to bound
187 187 */
188 188 public long nextLong(long least, long bound) {
189 189 if (least >= bound)
190 190 throw new IllegalArgumentException();
191 191 return nextLong(bound - least) + least;
192 192 }
193 193
194 194 /**
195 195 * Returns a pseudorandom, uniformly distributed {@code double} value
196 196 * between 0 (inclusive) and the specified value (exclusive).
197 197 *
198 198 * @param n the bound on the random number to be returned. Must be
199 199 * positive.
200 200 * @return the next value
201 201 * @throws IllegalArgumentException if n is not positive
202 202 */
203 203 public double nextDouble(double n) {
204 204 if (n <= 0)
205 205 throw new IllegalArgumentException("n must be positive");
206 206 return nextDouble() * n;
207 207 }
208 208
209 209 /**
210 210 * Returns a pseudorandom, uniformly distributed value between the
211 211 * given least value (inclusive) and bound (exclusive).
212 212 *
213 213 * @param least the least value returned
214 214 * @param bound the upper bound (exclusive)
215 215 * @return the next value
216 216 * @throws IllegalArgumentException if least greater than or equal
217 217 * to bound
218 218 */
219 219 public double nextDouble(double least, double bound) {
220 220 if (least >= bound)
221 221 throw new IllegalArgumentException();
222 222 return nextDouble() * (bound - least) + least;
223 223 }
224 224
225 225 private static final long serialVersionUID = -5851777807851030925L;
226 226 }
↓ open down ↓ |
148 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX