63 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
64 |
|
final java.lang.reflect.Method m; |
65 |
|
try { |
66 |
< |
m = ThreadLocalRandom.class.getDeclaredMethod( |
67 |
< |
"next", new Class[] { int.class }); |
66 |
> |
m = ThreadLocalRandom.class.getDeclaredMethod("next", int.class); |
67 |
|
m.setAccessible(true); |
68 |
|
} catch (SecurityException acceptable) { |
69 |
|
// Security manager may deny access |
354 |
|
// Don't use main thread's ThreadLocalRandom - it is likely to |
355 |
|
// be polluted by previous tests. |
356 |
|
final AtomicReference<ThreadLocalRandom> threadLocalRandom = |
357 |
< |
new AtomicReference<ThreadLocalRandom>(); |
357 |
> |
new AtomicReference<>(); |
358 |
|
final AtomicLong rand = new AtomicLong(); |
359 |
|
|
361 |
– |
long firstRand = 0; |
362 |
– |
ThreadLocalRandom firstThreadLocalRandom = null; |
363 |
– |
|
360 |
|
Runnable getRandomState = new CheckedRunnable() { |
361 |
|
public void realRun() { |
362 |
|
ThreadLocalRandom current = ThreadLocalRandom.current(); |
363 |
|
assertSame(current, ThreadLocalRandom.current()); |
368 |
– |
// test bug: the following is not guaranteed and not true in JDK8 |
369 |
– |
// assertNotSame(current, threadLocalRandom.get()); |
364 |
|
rand.set(current.nextLong()); |
365 |
|
threadLocalRandom.set(current); |
366 |
|
}}; |
367 |
|
|
368 |
< |
Thread first = newStartedThread(getRandomState); |
369 |
< |
awaitTermination(first); |
370 |
< |
firstRand = rand.get(); |
371 |
< |
firstThreadLocalRandom = threadLocalRandom.get(); |
368 |
> |
awaitTermination(newStartedThread(getRandomState)); |
369 |
> |
long firstRand = rand.get(); |
370 |
> |
ThreadLocalRandom firstThreadLocalRandom = threadLocalRandom.get(); |
371 |
> |
assertNotNull(firstThreadLocalRandom); |
372 |
|
|
373 |
|
for (int i = 0; i < NCALLS; i++) { |
374 |
< |
Thread t = newStartedThread(getRandomState); |
375 |
< |
awaitTermination(t); |
374 |
> |
awaitTermination(newStartedThread(getRandomState)); |
375 |
> |
if (testImplementationDetails) |
376 |
> |
// ThreadLocalRandom has been a singleton since jdk8. |
377 |
> |
assertSame(firstThreadLocalRandom, threadLocalRandom.get()); |
378 |
|
if (firstRand != rand.get()) |
379 |
|
return; |
380 |
|
} |