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


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


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

用法:

public final int accumulateAndGet(int i, int x, IntBinaryOperator accumulatorFunction)



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

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

返回值:該函數返回整數形式的更新值。

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

// Java program that demonstrates 
// the accumulateAndGet() function 
  
import java.util.concurrent.atomic.AtomicIntegerArray; 
import java.util.function.IntBinaryOperator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        int a[] = { 1, 2, 3, 4, 5 }; 
  
        // Initializing an AtomicIntegerArray with array a 
        AtomicIntegerArray arr = new AtomicIntegerArray(a); 
  
        // Displaying the AtomicIntegerArray 
        System.out.println("The array : " + arr); 
  
        // Index where update is to be made 
        int idx = 4; 
  
        // Value to make operation with value at idx 
        int x = 5; 
  
        // Declaring the accumulatorFunction 
        IntBinaryOperator add = (u, v) -> u + v; 
  
        // Updating the value at idx 
        // applying accumulatorFunction 
        arr.accumulateAndGet(idx, x, add); 
  
        // Displaying the AtomicIntegerArray 
        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.AtomicIntegerArray; 
import java.util.function.IntBinaryOperator; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
        // Initializing an array 
        int a[] = { 17, 22, 33, 44, 55 }; 
  
        // Initializing an AtomicIntegerArray with array a 
        AtomicIntegerArray arr = new AtomicIntegerArray(a); 
  
        // Displaying the AtomicIntegerArray 
        System.out.println("The array : " + arr); 
  
        // Index where update is to be made 
        int idx = 0; 
  
        // Value to make operation with value at idx 
        int x = 6; 
  
        // Declaring the accumulatorFunction 
        IntBinaryOperator sub = (u, v) -> u - v; 
  
        // Updating the value at idx 
        // applying accumulatorFunction 
        arr.accumulateAndGet(idx, x, sub); 
  
        // Displaying the AtomicIntegerArray 
        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/AtomicIntegerArray.html#getAndAccumulate-int-int-java.util.function.IntBinaryOperator-



相關用法


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