Java Code Examples for java.util.concurrent.atomic.LongAccumulator#accumulate()

The following examples show how to use java.util.concurrent.atomic.LongAccumulator#accumulate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongAccumulatorDemo.java    From java-concurrent-programming with MIT License 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    LongAccumulator accumulator = new LongAccumulator(Long::max, Long.MIN_VALUE);
    Thread[] ts = new Thread[1000];

    for(int i=0;i<1000;i++){
        ts[i] = new Thread(()->{
            Random random = new Random();
            long value = random.nextLong();
            accumulator.accumulate(value);
        });
        ts[i].start();
    }
    for(int i=0;i<1000;i++){
        ts[i].join();
    }
    System.out.println(accumulator.longValue());
}
 
Example 2
Source File: AccumulatorTest.java    From LearningOfThinkInJava with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception{
    LongAccumulator accumulator=new LongAccumulator(Long::max,Long.MIN_VALUE);
    Thread[] ts=new Thread[100];

    for(int i=0;i<100;i++){
        ts[i]=new Thread(()->{
            Random random=new Random();
            long value=random.nextLong();
            accumulator.accumulate(value);
        });
        ts[i].start();
    }

    for(int i=0;i<100;i++){
        ts[i].join();
    }
    System.out.println(accumulator.longValue());
}
 
Example 3
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * reset() causes subsequent get() to return zero
 */
public void testReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.reset();
    assertEquals(0, ai.get());
}
 
Example 4
Source File: Serial.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example 5
Source File: Serial.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example 6
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    LongAccumulator a = acc;
    for (int i = 0; i < incs; ++i)
        a.accumulate(i);
    result = a.get();
    phaser.arrive();
}
 
Example 7
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * doubleValue returns current value.
 */
public void testDoubleValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0.0, ai.doubleValue());
    ai.accumulate(1);
    assertEquals(1.0, ai.doubleValue());
}
 
Example 8
Source File: Serial.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example 9
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * longValue returns current value.
 */
public void testLongValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.longValue());
    ai.accumulate(1);
    assertEquals(1, ai.longValue());
}
 
Example 10
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * reset() causes subsequent get() to return zero
 */
public void testReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.reset();
    assertEquals(0, ai.get());
}
 
Example 11
Source File: LongAccumulatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * toString returns current value.
 */
public void testToString() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals("0", ai.toString());
    ai.accumulate(1);
    assertEquals(Long.toString(1), ai.toString());
}
 
Example 12
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void run() {
    phaser.arriveAndAwaitAdvance();
    LongAccumulator a = acc;
    for (int i = 0; i < incs; ++i)
        a.accumulate(i);
    result = a.get();
    phaser.arrive();
}
 
Example 13
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * longValue returns current value.
 */
public void testLongValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0, ai.longValue());
    ai.accumulate(1);
    assertEquals(1, ai.longValue());
}
 
Example 14
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * doubleValue returns current value.
 */
public void testDoubleValue() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    assertEquals(0.0, ai.doubleValue());
    ai.accumulate(1);
    assertEquals(1.0, ai.doubleValue());
}
 
Example 15
Source File: Serial.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example 16
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getThenReset() returns current value; subsequent get() returns zero
 */
public void testGetThenReset() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    assertEquals(2, ai.getThenReset());
    assertEquals(0, ai.get());
}
 
Example 17
Source File: Serial.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example 18
Source File: Serial.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example 19
Source File: LongAccumulatorTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * accumulate accumulates given value to current, and get returns current value
 */
public void testAccumulateAndGet() {
    LongAccumulator ai = new LongAccumulator(Long::max, 0L);
    ai.accumulate(2);
    assertEquals(2, ai.get());
    ai.accumulate(-4);
    assertEquals(2, ai.get());
    ai.accumulate(4);
    assertEquals(4, ai.get());
}
 
Example 20
Source File: Serial.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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");
}