Java DoubleAdder sumThenReset()方法及示例

Java DoubleAdder sumThenReset()方法及示例

java.DoubleAdder.sumThenReset() 是java中的一个内置方法,相当于sum()然后reset()方法,即首先计算有效和,然后重置数值以保持零值。当该类的对象被创建时,其初始值为零。

语法

public double sumThenReset()

参数: 该方法不接受任何参数。

返回值: 该方法返回总和。

下面的程序说明了上述方法。

程序1 :

// Program to demonstrate the sumThenReset() method
  
import java.lang.*;
import java.util.concurrent.atomic.DoubleAdder;
  
public class GFG {
    public static void main(String args[])
    {
        DoubleAdder num = new DoubleAdder();
  
        // add operation on num
        num.add(42);
        num.add(10);
  
        // sum operation on num
        num.sum();
  
        // Print after sum operation
        System.out.println(" the value after sum is: " + num);
  
        // sumThenReset operation on variable num
        num.sumThenReset();
  
        // Print after sumThenReset operation
        System.out.println("the current value is: " + num);
    }
}

输出:

the value after sum is: 52.0
the current value is: 0.0

程序2 :

// Program to demonstrate the sumThenReset() method
  
import java.lang.*;
import java.util.concurrent.atomic.DoubleAdder;
  
public class GFG {
    public static void main(String args[])
    {
        DoubleAdder num = new DoubleAdder();
  
        // add operation on num
        num.add(254);
  
        // sum operation on num
        num.sum();
  
        // Print after sum operation
        System.out.println(" the value after sum is: " + num);
  
        // sumThenReset operation on variable num
        num.sumThenReset();
  
        // Print after sumThenReset operation
        System.out.println("the current value is: " + num);
    }
}

输出:

the value after sum is: 254.0
the current value is: 0.0

参考资料 : https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/DoubleAdder.html#sumThenReset-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程