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


Java AtomicLong accumulateAndGet()用法及代碼示例


Java.AtomicLong.accumulateAndGet()方法是一種內置方法,該方法通過對當前值和作為參數傳遞的值應用指定的操作來更新對象的當前值。它以long作為其參數以及LongBinaryOperator接口的對象,並將該對象中指定的操作應用於這些值。它返回更新的值。

用法:

public final long accumulateAndGet(long y, 
              LongBinaryOperator function)

參數:此方法接受長值y和LongBinaryOperator函數作為參數。它將給定函數應用於對象的當前值和值y。


返回值:該函數返回當前對象的更新值。

實例演示函數。

// Java programt to demonstrate 
// AtomicLong accumulateAndGet() method 
  
import java.util.concurrent.atomic.AtomicLong; 
import java.util.function.LongBinaryOperator; 
  
public class Demo { 
    public static void main(String[] args) 
    { 
        // AtomicLong initialized with a value of 555 
        AtomicLong atomicLong = new AtomicLong(555); 
  
        // Binary operator defined 
        // to calculate the product 
        LongBinaryOperator binaryOperator 
            = (x, y) -> x * y; 
  
        System.out.println("Initial Value is "
                           + atomicLong); 
  
        // Function called and the binary operator 
        // is passed as an argument 
        long value 
            = atomicLong 
                  .accumulateAndGet(555, binaryOperator); 
        System.out.println("Updated value is "
                           + value); 
    } 
}
輸出:
Initial Value is 555
Updated value is 308025

參考: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLong.html



相關用法


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