Search in sources :

Example 1 with LongBinaryOperator

use of java.util.function.LongBinaryOperator in project java8-tutorial by winterbe.

the class LongAccumulator1 method testAccumulate.

private static void testAccumulate() {
    LongBinaryOperator op = (x, y) -> 2 * x + y;
    LongAccumulator accumulator = new LongAccumulator(op, 1L);
    ExecutorService executor = Executors.newFixedThreadPool(2);
    IntStream.range(0, 10).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
    ConcurrentUtils.stop(executor);
    System.out.format("Add: %d\n", accumulator.getThenReset());
}
Also used : IntStream(java.util.stream.IntStream) LongAccumulator(java.util.concurrent.atomic.LongAccumulator) LongBinaryOperator(java.util.function.LongBinaryOperator) ExecutorService(java.util.concurrent.ExecutorService) Executors(java.util.concurrent.Executors) LongAccumulator(java.util.concurrent.atomic.LongAccumulator) LongBinaryOperator(java.util.function.LongBinaryOperator) ExecutorService(java.util.concurrent.ExecutorService)

Example 2 with LongBinaryOperator

use of java.util.function.LongBinaryOperator in project jdk8u_jdk by JetBrains.

the class PrimitiveSumMinMaxTest method testLongMethods.

public void testLongMethods() {
    BinaryOperator<Long> sum1 = Long::sum;
    LongBinaryOperator sum2 = Long::sum;
    BinaryOperator<Long> max1 = Long::max;
    LongBinaryOperator max2 = Long::max;
    BinaryOperator<Long> min1 = Long::min;
    LongBinaryOperator min2 = Long::min;
    Comparator<Long> cmp = Long::compare;
    long[] numbers = { -1, 0, 1, 100, Long.MAX_VALUE, Long.MIN_VALUE };
    for (long i : numbers) {
        for (long j : numbers) {
            assertEquals(i + j, (long) sum1.apply(i, j));
            assertEquals(i + j, sum2.applyAsLong(i, j));
            assertEquals(Math.max(i, j), (long) max1.apply(i, j));
            assertEquals(Math.max(i, j), max2.applyAsLong(i, j));
            assertEquals(Math.min(i, j), (long) min1.apply(i, j));
            assertEquals(Math.min(i, j), min2.applyAsLong(i, j));
            assertEquals(((Long) i).compareTo(j), cmp.compare(i, j));
        }
    }
}
Also used : LongBinaryOperator(java.util.function.LongBinaryOperator)

Example 3 with LongBinaryOperator

use of java.util.function.LongBinaryOperator in project jdk8u_jdk by JetBrains.

the class Serial method testLongAccumulator.

static void testLongAccumulator() {
    LongBinaryOperator plus = (LongBinaryOperator & Serializable) (x, y) -> x + y;
    LongAccumulator a = new LongAccumulator(plus, -2);
    a.accumulate(34);
    LongAccumulator result = echo(a);
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value");
    a.reset();
    result.reset();
    if (result.get() != a.get())
        throw new RuntimeException("Unexpected value after reset");
    checkSerialClassName(a, "java.util.concurrent.atomic.LongAccumulator$SerializationProxy");
}
Also used : LongAccumulator(java.util.concurrent.atomic.LongAccumulator) LongBinaryOperator(java.util.function.LongBinaryOperator)

Aggregations

LongBinaryOperator (java.util.function.LongBinaryOperator)3 LongAccumulator (java.util.concurrent.atomic.LongAccumulator)2 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 IntStream (java.util.stream.IntStream)1