當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。