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


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


Java.util.concurrent.atomic.AtomicLongArray.accumulateAndGet()是Java中的一種內置方法,該方法原子地更新索引i的元素,其結果是將給定函數應用於當前值和給定值,並返回更新後的值。該函數應無副作用,因為當嘗試更新由於線程間爭用而失敗時,可能會重新應用該函數。應用該函數時,將索引i處的當前值作為其第一個參數,並將給定的update作為第二個參數。

用法:

public final long accumulateAndGet(int i, long x, LongBinaryOperator accumulatorFunction)



參數:該函數接受三個參數:

  • i–將要更新的索引。
  • x–以i處的值進行運算的值
  • accumulatorFunction–兩個參數的無副作用函數。

返回值:函數返回的更新值過長。

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

// Java program that demonstrates 
// the accumulateAndGet() function 
  
import java.util.concurrent.atomic.AtomicLongArray; 
import java.util.function.LongBinaryOperator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        long a[] = { 1, 2, 3, 4, 5 }; 
  
        // Initializing an AtomicLongArray with array a 
        AtomicLongArray arr = new AtomicLongArray(a); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array : " + arr); 
  
        // Index where update is to be made 
        int idx = 4; 
  
        // Value to make operation with value at idx 
        long x = 5; 
  
        // Declaring the accumulatorFunction 
        LongBinaryOperator add = (u, v) -> u + v; 
  
        // Updating the value at idx 
        // applying accumulatorFunction 
        arr.accumulateAndGet(idx, x, add); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
輸出:
The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 4, 10]

示例2:

// Java program that demonstrates 
// the accumulateAndGet() function 
  
import java.util.concurrent.atomic.AtomicLongArray; 
import java.util.function.LongBinaryOperator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        long a[] = { 17, 22, 33, 44, 55 }; 
  
        // Initializing an AtomicLongArray with array a 
        AtomicLongArray arr = new AtomicLongArray(a); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array : " + arr); 
  
        // Index where update is to be made 
        int idx = 0; 
  
        // Value to make operation with value at idx 
        long x = 6; 
  
        // Declaring the accumulatorFunction 
        LongBinaryOperator sub = (u, v) -> u - v; 
  
        // Updating the value at idx 
        // applying accumulatorFunction 
        arr.accumulateAndGet(idx, x, sub); 
  
        // Displaying the AtomicLongArray 
        System.out.println("The array after update : "
                           + arr); 
    } 
}
輸出:
The array : [17, 22, 33, 44, 55]
The array after update : [11, 22, 33, 44, 55]

參考:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#accumulateAndGet-int-long-java.util.function.LongBinaryOperator-



相關用法


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