use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testNextBytes.
/**
* Repeated calls to nextBytes produce at least values of different signs for every byte
*/
public void testNextBytes() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
int n = rnd.nextInt(1, 20);
byte[] bytes = new byte[n];
outer: for (int i = 0; i < n; i++) {
for (int tries = NCALLS; tries-- > 0; ) {
byte before = bytes[i];
rnd.nextBytes(bytes);
byte after = bytes[i];
if (after * before < 0)
continue outer;
}
fail("not enough variation in random bytes");
}
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testNextDoubleBoundNonPositive.
/**
* nextDouble(non-positive) throws IllegalArgumentException
*/
public void testNextDoubleBoundNonPositive() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
double[] badBounds = { 0.0d, -17.0d, -Double.MIN_VALUE, Double.NEGATIVE_INFINITY, Double.NaN };
for (double bound : badBounds) {
try {
rnd.nextDouble(bound);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testNextIntBadBounds.
/**
* nextInt(least >= bound) throws IllegalArgumentException
*/
public void testNextIntBadBounds() {
int[][] badBoundss = { { 17, 2 }, { -42, -42 }, { Integer.MAX_VALUE, Integer.MIN_VALUE } };
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int[] badBounds : badBoundss) {
try {
rnd.nextInt(badBounds[0], badBounds[1]);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class LinkedTransferQueue method awaitMatch.
/**
* Spins/yields/blocks until node s is matched or caller gives up.
*
* @param s the waiting node
* @param pred the predecessor of s, or s itself if it has no
* predecessor, or null if unknown (the null case does not occur
* in any current calls but may in possible future extensions)
* @param e the comparison value for checking match
* @param timed if true, wait only until timeout elapses
* @param nanos timeout in nanosecs, used only if timed is true
* @return matched item, or e if unmatched on interrupt or timeout
*/
private E awaitMatch(Node s, Node pred, E e, boolean timed, long nanos) {
long lastTime = timed ? System.nanoTime() : 0L;
Thread w = Thread.currentThread();
// initialized after first item and cancel checks
int spins = -1;
// bound if needed
ThreadLocalRandom randomYields = null;
for (; ; ) {
Object item = s.item;
if (item != e) {
// matched
// assert item != s;
// avoid garbage
s.forgetContents();
return this.<E>cast(item);
}
if ((w.isInterrupted() || (timed && nanos <= 0)) && s.casItem(e, s)) {
// cancel
unsplice(pred, s);
return e;
}
if (spins < 0) {
// establish spins at/near front
if ((spins = spinsFor(pred, s.isData)) > 0)
randomYields = ThreadLocalRandom.current();
} else if (spins > 0) {
// spin
--spins;
if (randomYields.nextInt(CHAINED_SPINS) == 0)
// occasionally yield
Thread.yield();
} else if (s.waiter == null) {
// request unpark then recheck
s.waiter = w;
} else if (timed) {
long now = System.nanoTime();
if ((nanos -= now - lastTime) > 0)
LockSupport.parkNanos(this, nanos);
lastTime = now;
} else {
LockSupport.park(this);
}
}
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testUnsizedDoublesCount.
/**
* A parallel unsized stream of doubles generates at least 100 values
*/
public void testUnsizedDoublesCount() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 100;
r.doubles().limit(size).parallel().forEach(x -> {
counter.increment();
});
assertEquals(counter.sum(), size);
}
Aggregations