java.util.concurrent.atomic.LongAccumulator#accumulate()源码实例Demo

下面列出了java.util.concurrent.atomic.LongAccumulator#accumulate() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

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());
}
 
源代码2 项目: LearningOfThinkInJava   文件: AccumulatorTest.java
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());
}
 
源代码3 项目: j2objc   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码4 项目: openjdk-8-source   文件: Serial.java
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");
}
 
源代码5 项目: j2objc   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码6 项目: jdk8u60   文件: Serial.java
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");
}
 
源代码7 项目: openjdk-jdk8u   文件: Serial.java
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");
}
 
源代码8 项目: j2objc   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码9 项目: openjdk-jdk9   文件: Serial.java
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");
}
 
源代码10 项目: j2objc   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码11 项目: openjdk-jdk9   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码12 项目: j2objc   文件: LongAccumulatorTest.java
public void run() {
    phaser.arriveAndAwaitAdvance();
    LongAccumulator a = acc;
    for (int i = 0; i < incs; ++i)
        a.accumulate(i);
    result = a.get();
    phaser.arrive();
}
 
源代码13 项目: openjdk-jdk9   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码14 项目: j2objc   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码15 项目: openjdk-jdk9   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码16 项目: jdk8u-jdk   文件: Serial.java
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");
}
 
源代码17 项目: openjdk-jdk9   文件: LongAccumulatorTest.java
/**
 * 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());
}
 
源代码18 项目: openjdk-jdk9   文件: LongAccumulatorTest.java
public void run() {
    phaser.arriveAndAwaitAdvance();
    LongAccumulator a = acc;
    for (int i = 0; i < incs; ++i)
        a.accumulate(i);
    result = a.get();
    phaser.arrive();
}
 
源代码19 项目: jdk8u-jdk   文件: Serial.java
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");
}
 
源代码20 项目: openjdk-8   文件: Serial.java
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");
}