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


Java LongAccumulator get()用法及代碼示例


java.LongAccumulator.get()是java中的一個內置方法,該方法返回LongAccumulator對象的當前值。

用法:

public long get()

參數:此方法不接受任何參數。


返回值:該方法返回LongAccumulator對象的當前值。

以下示例程序旨在說明上述方法:

程序1

// Program to demonstrate the get() method 
  
import java.lang.*; 
import java.util.concurrent.atomic.LongAccumulator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        LongAccumulator num = new LongAccumulator(Long::sum, 0L); 
  
        // accumulate operation on num 
        num.accumulate(2); 
        num.accumulate(10); 
  
        // Gets current value 
        long x = num.get(); 
  
        // Print after get operation 
        System.out.println(" the current value is: " + x); 
    } 
}
輸出:
the current value is: 12

程序2

// Program to demonstrate the get() method 
  
import java.lang.*; 
import java.util.concurrent.atomic.LongAccumulator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        LongAccumulator num = new LongAccumulator(Long::sum, 0L); 
  
        // accumulate operation on num 
        num.accumulate(22); 
        num.accumulate(10); 
  
        // Gets current value 
        long x = num.get(); 
  
        // Print after get operation 
        System.out.println(" the current value is: " + x); 
    } 
}
輸出:
the current value is: 32

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/LongAccumulator.html#get–



相關用法


注:本文由純淨天空篩選整理自Twinkl Bajaj大神的英文原創作品 LongAccumulator get() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。