Java中的 DoubleAccumulator getThenReset() 方法及示例
Java.DoubleAccumulator.getThenReset()是Java中的一个内置方法,其效果等同于 get() 后跟 reset()。首先,它获取当前值,然后重置该值。复位值为零。返回类型是 int。
句法:
public double getThenReset()
参数:该方法不接受任何参数。
返回值:该方法返回重置前的值。
下面的程序说明了上述方法:
方案一:
// Java program to demonstrate
// the getThenReset() method
import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
public class GFG {
public static void main(String args[])
{
DoubleAccumulator num
= new DoubleAccumulator(
Double::sum, 0L);
// accumulate operation on num
num.accumulate(42);
num.accumulate(10);
num.get();
// before getThenReset the value is
System.out.println("Old value is: "
+ num);
// getThenResets current value
num.getThenReset();
// Print after getThenReset operation
System.out.println("Current value is: "
+ num);
}
}
输出:
Old value is: 52.0
Current value is: 0.0
方案二:
// Java program to demonstrate
// the getThenReset() method import java.lang.*;
import java.util.concurrent.atomic.DoubleAccumulator;
public class GFG {
public static void main(String args[])
{
DoubleAccumulator num
= new DoubleAccumulator(
Double::sum, 0L);
// accumulate operation on num
num.accumulate(74);
num.accumulate(1);
num.get();
// before getThenReset the value is
System.out.println("Old value is: "
+ num);
// getThenResets current value
num.getThenReset();
// Print after getThenReset operation
System.out.println("Current value is: "
+ num);
}
}
输出:
Old value is: 75.0
Current value is: 0.0