当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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–



相关用法


注:本文由纯净天空筛选整理自Twinkl Bajaj大神的英文原创作品 DoubleAdder sumThenReset() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。