AtomicIntegerArray accumulateAndGet() method in Java with Examples - GeeksforGeeks

27 Feb, 2019
The Java.util.concurrent.atomic.AtomicIntegerArray.accumulateAndGet() is an inbuilt method in java that atomically updates the element at index i with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value at index i as its first argument, and the given update as the second argument.
Syntax:
public final int accumulateAndGet(int i, int x, IntBinaryOperator accumulatorFunction)
Parameters: The function accepts three parameters:
i – The index where update is to be mada.
x – The value to make operation with value at i
accumulatorFunction – A side-effect-free function of two arguments.Return value: The function returns the updated value which is in Integer.Below programs illustrate the above method:Program 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);    }}Output:The array : [1, 2, 3, 4, 5]
The array after update : [1, 2, 3, 4, 10]
Program 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);    }}Output:The array : [17, 22, 33, 44, 55]
The array after update : [11, 22, 33, 44, 55]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#getAndAccumulate-int-int-java.util.function.IntBinaryOperator-My Personal Notes
arrow_drop_upSave
Return value: The function returns the updated value which is in Integer.
Below programs illustrate the above method:Program 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]

Program 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]

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicIntegerArray.html#getAndAccumulate-int-int-java.util.function.IntBinaryOperator-
Java-AtomicIntegerArray
Java-concurrent-package
Java-Functions
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java