Search in sources :

Example 21 with ThreadLocalRandom

use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testBadStreamSize.

/**
 * Invoking sized ints, long, doubles, with negative sizes throws
 * IllegalArgumentException
 */
public void testBadStreamSize() {
    ThreadLocalRandom r = ThreadLocalRandom.current();
    executeAndCatchIAE(() -> r.ints(-1L));
    executeAndCatchIAE(() -> r.ints(-1L, 2, 3));
    executeAndCatchIAE(() -> r.longs(-1L));
    executeAndCatchIAE(() -> r.longs(-1L, -1L, 1L));
    executeAndCatchIAE(() -> r.doubles(-1L));
    executeAndCatchIAE(() -> r.doubles(-1L, .5, .6));
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 22 with ThreadLocalRandom

use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testUnsizedIntsCount.

/**
 * A parallel unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 100;
    r.ints().limit(size).parallel().forEach(x -> {
        counter.increment();
    });
    assertEquals(counter.sum(), size);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 23 with ThreadLocalRandom

use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testIntsCount.

/**
 * A parallel sized stream of ints generates the given number of values
 */
public void testIntsCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.ints(size).parallel().forEach(x -> {
            counter.increment();
        });
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 24 with ThreadLocalRandom

use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testUnsizedLongsCount.

/**
 * A parallel unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 100;
    r.longs().limit(size).parallel().forEach(x -> {
        counter.increment();
    });
    assertEquals(counter.sum(), size);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 25 with ThreadLocalRandom

use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testDoublesCount.

/**
 * A parallel sized stream of doubles generates the given number of values
 */
public void testDoublesCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.doubles(size).parallel().forEach(x -> {
            counter.increment();
        });
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
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