Java中的 LongAccumulator reset() 方法及示例
Java.LongAccumulator.reset()是Java中的一个内置方法,它重置变量以保持对标识值的更新。
句法:
public void reset()
参数:该方法不接受任何参数。
返回值:该方法返回复位值。
下面的程序说明了上述方法:
程序 1 :
// Program to demonstrate the reset() 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);
// resets current value
num.reset();
// Print after reset operation
System.out.println(" the current value is: " + num);
}
}
输出:
the current value is: 0
方案二:
// Program to demonstrate the reset() 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(42);
num.accumulate(10);
// resets current value
num.reset();
// Print after reset operation
System.out.println(" the current value is: " + num);
}
}
输出:
the current value is: 0
参考:https: Java/util/concurrent/atomic/LongAccumulator.html#reset–