Java中的 LongAccumulator get() 方法及示例
Java.LongAccumulator.get()是Java中的一个内置方法,它返回 LongAccumulator 对象的当前值。
句法:
public long get()
参数:此方法不接受任何参数。
返回值:该方法返回 LongAccumulator 对象的当前值。
下面的程序说明了上述方法:
程序 1 :
// Program to demonstrate the get() 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);
num.accumulate(10);
// Gets current value
long x = num.get();
// Print after get operation
System.out.println(" the current value is: " + x);
}
}
输出:
the current value is: 12
方案二:
// Program to demonstrate the get() 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(22);
num.accumulate(10);
// Gets current value
long x = num.get();
// Print after get operation
System.out.println(" the current value is: " + x);
}
}
输出:
the current value is: 32
参考:https: Java/util/concurrent/atomic/LongAccumulator.html#get–