Search in sources :

Example 66 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project aries by apache.

the class AbstractPushStreamImpl method forEachEvent.

@Override
public Promise<Long> forEachEvent(PushEventConsumer<? super T> action) {
    Deferred<Long> d = new Deferred<>();
    LongAdder la = new LongAdder();
    updateNext((event) -> {
        try {
            switch(event.getType()) {
                case DATA:
                    long value = action.accept(event);
                    la.add(value);
                    return value;
                case CLOSE:
                    try {
                        action.accept(event);
                    } finally {
                        d.resolve(Long.valueOf(la.sum()));
                    }
                    break;
                case ERROR:
                    try {
                        action.accept(event);
                    } finally {
                        d.fail(event.getFailure());
                    }
                    break;
            }
            return ABORT;
        } catch (Exception e) {
            close(PushEvent.error(e));
            return ABORT;
        }
    });
    begin();
    return d.getPromise();
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) Deferred(org.osgi.util.promise.Deferred) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException)

Example 67 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project aries by apache.

the class AbstractPushStreamImpl method count.

@Override
public Promise<Long> count() {
    Deferred<Long> d = new Deferred<>();
    LongAdder counter = new LongAdder();
    updateNext((event) -> {
        try {
            switch(event.getType()) {
                case DATA:
                    counter.add(1);
                    return CONTINUE;
                case CLOSE:
                    d.resolve(Long.valueOf(counter.sum()));
                    break;
                case ERROR:
                    d.fail(event.getFailure());
                    break;
            }
            close(event.nodata());
            return ABORT;
        } catch (Exception e) {
            close(PushEvent.error(e));
            return ABORT;
        }
    });
    begin();
    return d.getPromise();
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) Deferred(org.osgi.util.promise.Deferred) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentModificationException(java.util.ConcurrentModificationException) NoSuchElementException(java.util.NoSuchElementException)

Example 68 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testUnsizedIntsCountSeq.

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

Example 69 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testUnsizedDoublesCount.

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

Example 70 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testUnsizedIntsCount.

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

Aggregations

LongAdder (java.util.concurrent.atomic.LongAdder)92 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Random (java.util.Random)13 ExecutorService (java.util.concurrent.ExecutorService)10 SplittableRandom (java.util.SplittableRandom)9 HashMap (java.util.HashMap)7 Map (java.util.Map)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Theory (org.junit.experimental.theories.Theory)6 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Ignite (org.apache.ignite.Ignite)4 IgniteUuid (org.apache.ignite.lang.IgniteUuid)4 Metric (org.springframework.boot.actuate.metrics.Metric)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Callable (java.util.concurrent.Callable)3 CountDownLatch (java.util.concurrent.CountDownLatch)3