AtomicLongArray accumulateAndGet() method in Java with Examples Last Updated : 05 Feb, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The Java.util.concurrent.atomic.AtomicLongArray.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 long accumulateAndGet(int i, long x, LongBinaryOperator 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 long. Below programs illustrate the above method: Program 1: Java // 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); } } Output: The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 4, 10] Program 2: Java // 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); } } 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/AtomicLongArray.html#accumulateAndGet-int-long-java.util.function.LongBinaryOperator- Comment More infoAdvertise with us Next Article AtomicInteger accumulateAndGet() method in Java with Examples R rupesh_rao Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicLongArray Practice Tags : Java Similar Reads AtomicLong accumulateAndGet() method in Java with Examples The Java.AtomicLong.accumulateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value and value passed as a parameter. It takes a long as its parameter and an object of LongBinaryOperator interface and applies the o 2 min read AtomicIntegerArray accumulateAndGet() method in Java with Examples 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, sin 3 min read AtomicInteger accumulateAndGet() method in Java with Examples The Java.AtomicInteger.accumulateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value and value passed as a parameter. It takes an integer as its parameter and an object of IntBinaryOperator interface and applies 2 min read AtomicReferenceArray accumulateAndGet() method in Java with Examples The accumulateAndGet() method of a AtomicReferenceArray class is used to atomically updates the element at index i of AtomicReferenceArray with the results of applying the given accumulatorFunction to the current and given values and returns the updated value. The accumulatorFunction should be side- 2 min read AtomicLongArray addAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.addAndGet() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicLongArray. This method takes the index value and the value to be added as the parameters and returns the updated value at this index. 2 min read AtomicReference accumulateAndGet() method in Java with Examples The accumulateAndGet() method of a AtomicReference class is used to atomically updates the current value of AtomicReference with the results of applying the given accumulatorFunction to the current and given values and returns the updated value. The accumulatorFunction should be side-effect-free, si 2 min read Like