Java中的LongAccumulator accumulator()方法及示例
Java.LongAccumulator.accumulate()是Java中的一个内置方法,它使用给定的值更新特定的 LongAccumulator 对象。
句法:
public void accumulate(long x)
参数:该方法接受单个参数x ,即更新当前 LongAccumulator 对象的值。
返回值:该方法返回更新后的值。
下面的程序说明了上述方法:
程序 1 :
// Program to demonstrate the accumulate() method
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
public class GFG {
public static void main(String args[])
{
LongAccumulator num = new LongAccumulator(Long::sum, 0L);
// accumulate operation on num
num.accumulate(2);
// Print after accumulate
System.out.println("After update the value is: " + num);
}
}
输出:
After update the value is: 2
方案二:
// Program to demonstrate the accumulate() method
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
public class GFG {
public static void main(String args[])
{
LongAccumulator num = new LongAccumulator(Long::sum, 0L);
// accumulate operation on num
num.accumulate(5);
// Print after accumulate
System.out.println("After update the value is: " + num);
}
}
输出:
After update the value is: 5
参考:https: Java/util/concurrent/atomic/LongAccumulator.html#accumulate-long-