Java DoubleAccumulator getThenReset()方法及示例
Java.DoubleAccumulator.getThenReset() 是java中的一个内置方法,其作用相当于get()和reset()。首先,它得到当前值,然后重置该值。重置后的值为零。其返回类型为int。
语法
public double getThenReset()
参数: 该方法不接受任何参数。
返回值: 该方法返回重置前的值。
下面的程序说明了上述方法。
程序1 :
// 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
程序2
// 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
极客教程