Search in sources :

Example 46 with ThreadLocalRandom

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

Example 47 with ThreadLocalRandom

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

Example 48 with ThreadLocalRandom

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) {
        }
    }
}
Also used : 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