Search in sources :

Example 11 with ThreadLocalRandom

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");
    }
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 12 with ThreadLocalRandom

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) {
        }
    }
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 13 with ThreadLocalRandom

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) {
        }
    }
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 14 with ThreadLocalRandom

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);
        }
    }
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 15 with ThreadLocalRandom

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);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Aggregations

ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)48 LongAdder (java8.util.concurrent.atomic.LongAdder)18 Collection (java.util.Collection)9 Test (org.testng.annotations.Test)9 ArrayList (java.util.ArrayList)8 LinkedList (java.util.LinkedList)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 ArrayDeque (java.util.ArrayDeque)7 Deque (java.util.Deque)7 HashSet (java.util.HashSet)7 List (java.util.List)7 BlockingDeque (java.util.concurrent.BlockingDeque)7 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)7 ConcurrentModificationException (java.util.ConcurrentModificationException)6 Set (java.util.Set)6 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)6 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Spliterator (java8.util.Spliterator)6