use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandom8Test method testUnsizedLongsCountSeq.
/**
* A sequential unsized stream of longs generates at least 100 values
*/
public void testUnsizedLongsCountSeq() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 100;
r.longs().limit(size).forEach(x -> counter.increment());
assertEquals(size, counter.sum());
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testDifferentSequences.
/**
* Different threads produce different pseudo-random sequences
*/
public void testDifferentSequences() {
// Don't use main thread's ThreadLocalRandom - it is likely to
// be polluted by previous tests.
final AtomicReference<ThreadLocalRandom> threadLocalRandom = new AtomicReference<>();
final AtomicLong rand = new AtomicLong();
long firstRand = 0;
Runnable getRandomState = new CheckedRunnable() {
public void realRun() {
ThreadLocalRandom current = ThreadLocalRandom.current();
assertSame(current, ThreadLocalRandom.current());
// test bug: the following is not guaranteed and not true in JDK8
// assertNotSame(current, threadLocalRandom.get());
rand.set(current.nextLong());
threadLocalRandom.set(current);
}
};
Thread first = newStartedThread(getRandomState);
awaitTermination(first);
firstRand = rand.get();
@SuppressWarnings("unused") ThreadLocalRandom firstThreadLocalRandom = threadLocalRandom.get();
for (int i = 0; i < NCALLS; i++) {
Thread t = newStartedThread(getRandomState);
awaitTermination(t);
if (firstRand != rand.get())
return;
}
fail("all threads generate the same pseudo-random sequence");
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testNextIntBoundNonPositive.
/**
* nextInt(non-positive) throws IllegalArgumentException
*/
public void testNextIntBoundNonPositive() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int bound : new int[] { 0, -17, Integer.MIN_VALUE }) {
try {
rnd.nextInt(bound);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
}
Aggregations