Example usage for java.util.concurrent.atomic AtomicLong updateAndGet

List of usage examples for java.util.concurrent.atomic AtomicLong updateAndGet

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicLong updateAndGet.

Prototype

public final long updateAndGet(LongUnaryOperator updateFunction) 

Source Link

Document

Atomically updates (with memory effects as specified by VarHandle#compareAndSet ) the current value with the results of applying the given function, returning the updated value.

Usage

From source file:nl.rivm.cib.episim.model.disease.infection.MSEIRSTest.java

private <T> Observable<Entry<T, Stream<BigDecimal>>> averages(
        final Supplier<Observable<Entry<Double, long[]>>> sir, final Function<Double, T> bins, final int n) {
    return Observable.create(sub -> {
        final NavigableMap<T, long[]> sums = java.util.Collections
                .synchronizedNavigableMap(new TreeMap<T, long[]>());
        final long t0 = System.currentTimeMillis();
        final AtomicInteger iteration = new AtomicInteger();
        final AtomicLong sysTime = new AtomicLong(t0);
        Observable.range(0, n).flatMap(i -> Observable.just(i).subscribeOn(Schedulers.computation()).map(ii -> {
            final int iii = iteration.incrementAndGet();
            final long t = System.currentTimeMillis();
            sysTime.updateAndGet(t1 -> {
                if (t - t1 > 10000) {
                    LOG.trace("Progress {}% at ~{}/s, iteration {} of {}",
                            DecimalUtil.floor(DecimalUtil.divide(iii * 100, n)),
                            DecimalUtil.round(DecimalUtil.divide(iii * 1000, t - t0)), iii, n);
                    return t;
                }/*from  ww  w .j  a  va 2 s .c o  m*/
                return t1;
            });
            return sir.get()
                    // group by bin size
                    .groupBy(yt -> bins.apply(yt.getKey()))
                    // take highest floating point t in this bin
                    .flatMap(gr -> gr.reduce((yt1, yt2) -> yt1.getKey().compareTo(yt2.getKey()) > 0 ? yt1 : yt2)
                            .toObservable().map(yt -> Collections.entry(gr.getKey(), yt.getValue())))
                    // add to current sums
                    .collect(() -> sums,
                            (sum, yt) -> sum.compute(yt.getKey(),
                                    (k, v) -> v == null ? yt.getValue()
                                            : IntStream.range(0, v.length)
                                                    .mapToLong(iv -> v[iv] + yt.getValue()[iv]).toArray()))
                    .blockingGet();
        })).blockingSubscribe();

        sums.forEach((k, v) -> sub
                .onNext(Collections.entry(k, Arrays.stream(v).mapToObj(y -> DecimalUtil.divide(y, n)))));
        final long dt = System.currentTimeMillis() - t0;
        LOG.trace("Completed {} iterations in {}s = {}/s", n,
                DecimalUtil.toScale(DecimalUtil.divide(dt, 1000), 1),
                DecimalUtil.round(DecimalUtil.divide(n * 1000, dt)));
    });
}