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


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


相关用法


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