4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
import junit.framework.*; |
7 |
– |
import java.util.concurrent.*; |
7 |
|
import java.util.*; |
8 |
< |
|
8 |
> |
import java.util.concurrent.ThreadLocalRandom; |
9 |
> |
import java.util.concurrent.atomic.AtomicLong; |
10 |
> |
import java.util.concurrent.atomic.AtomicReference; |
11 |
|
|
12 |
|
public class ThreadLocalRandomTest extends JSR166TestCase { |
13 |
|
|
68 |
|
assertTrue(i < NCALLS); |
69 |
|
} |
70 |
|
|
70 |
– |
|
71 |
|
/** |
72 |
|
* Repeated calls to nextBoolean produce at least one different result |
73 |
|
*/ |
112 |
|
assertTrue(i < NCALLS); |
113 |
|
} |
114 |
|
|
115 |
– |
|
115 |
|
/** |
116 |
|
* nextInt(negative) throws IllegalArgumentException; |
117 |
|
*/ |
132 |
|
} catch (IllegalArgumentException success) {} |
133 |
|
} |
134 |
|
|
136 |
– |
|
135 |
|
/** |
136 |
|
* nextInt(bound) returns 0 <= value < bound; |
137 |
|
* repeated calls produce at least one different result |
152 |
|
} |
153 |
|
} |
154 |
|
|
157 |
– |
|
155 |
|
/** |
156 |
|
* nextInt(least, bound) returns least <= value < bound; |
157 |
|
* repeated calls produce at least one different result |
233 |
|
} |
234 |
|
} |
235 |
|
|
239 |
– |
|
236 |
|
/** |
237 |
|
* nextDouble(least, bound) returns least <= value < bound; |
238 |
|
* repeated calls produce at least one different result |
254 |
|
} |
255 |
|
} |
256 |
|
|
257 |
+ |
/** |
258 |
+ |
* Different threads produce different pseudo-random sequences |
259 |
+ |
*/ |
260 |
+ |
public void testDifferentSequences() { |
261 |
+ |
// Don't use main thread's ThreadLocalRandom - it is likely to |
262 |
+ |
// be polluted by previous tests. |
263 |
+ |
final AtomicReference<ThreadLocalRandom> threadLocalRandom = |
264 |
+ |
new AtomicReference<ThreadLocalRandom>(); |
265 |
+ |
final AtomicLong rand = new AtomicLong(); |
266 |
+ |
|
267 |
+ |
long firstRand = 0; |
268 |
+ |
ThreadLocalRandom firstThreadLocalRandom = null; |
269 |
+ |
|
270 |
+ |
final CheckedRunnable getRandomState = new CheckedRunnable() { |
271 |
+ |
public void realRun() { |
272 |
+ |
ThreadLocalRandom current = ThreadLocalRandom.current(); |
273 |
+ |
assertSame(current, ThreadLocalRandom.current()); |
274 |
+ |
// test bug: the following is not guaranteed and not true in JDK8 |
275 |
+ |
// assertNotSame(current, threadLocalRandom.get()); |
276 |
+ |
rand.set(current.nextLong()); |
277 |
+ |
threadLocalRandom.set(current); |
278 |
+ |
}}; |
279 |
+ |
|
280 |
+ |
Thread first = newStartedThread(getRandomState); |
281 |
+ |
awaitTermination(first); |
282 |
+ |
firstRand = rand.get(); |
283 |
+ |
firstThreadLocalRandom = threadLocalRandom.get(); |
284 |
+ |
|
285 |
+ |
for (int i = 0; i < NCALLS; i++) { |
286 |
+ |
Thread t = newStartedThread(getRandomState); |
287 |
+ |
awaitTermination(t); |
288 |
+ |
if (firstRand != rand.get()) |
289 |
+ |
return; |
290 |
+ |
} |
291 |
+ |
fail("all threads generate the same pseudo-random sequence"); |
292 |
+ |
} |
293 |
|
|
294 |
|
} |