AtomicReferenceArray getAndAccumulate() method in Java with Examples Last Updated : 03 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The getAndAccumulate() 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 previous value. The accumulatorFunction 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 E getAndAccumulate(int i, E x, BinaryOperator<E> accumulatorFunction) Parameters: This method accepts: i which is an index of AtomicReferenceArray to perform the operation accepts, x which is the updated value and accumulatorFunction which is a side-effect-free function of two arguments. Return value: This method returns the previous value. Below programs illustrate the getAndAccumulate() method: Program 1: Java // Java program to demonstrate // getAndAccumulate() method import java.util.concurrent.atomic.*; import java.util.function.BinaryOperator; public class GFG { public static void main(String args[]) { // an array Integer a[] = { 123, 1232, 1433, 134, 13415, 1343 }; // AtomicReferenceArray with array AtomicReferenceArray<Integer> array = new AtomicReferenceArray<>(a); // Print AtomicReferenceArray System.out.println( "The AtomicReferenceArray before update: " + array); // Index and Value to apply getAndAccumulate int index = 2; int E = 3; // Declaring the accumulatorFunction // applying function BinaryOperator add = (u, v) -> Integer.parseInt( u.toString()) * Integer .parseInt( v.toString()); // apply getAndAccumulate() int value = array.getAndAccumulate(index, E, add); // print AtomicReferenceArray System.out.println( "previous value of index 2:" + value); System.out.println( "The AtomicReferenceArray after update: " + array); } } Output: Program 2: Java // Java program to demonstrate // getAndAccumulate() method import java.util.concurrent.atomic.*; import java.util.function.BinaryOperator; public class GFG { public static void main(String args[]) { // an array String a[] = { "GFG", "JS" }; // AtomicReferenceArray with array AtomicReferenceArray<String> array = new AtomicReferenceArray<>(a); // Print AtomicReferenceArray System.out.println( "The AtomicReferenceArray before update : " + array); // Index and Value to apply getAndAccumulate int index = 1; String E = " PYTHON"; // Declaring the accumulatorFunction // applying function to add value as string BinaryOperator add = (u, v) -> u.toString() + " and " + v.toString(); // apply getAndAccumulate() String value = array.getAndAccumulate(index, E, add); // print AtomicReferenceArray System.out.println( "previous value of index 1:" + value); System.out.println( "The AtomicReferenceArray after update: " + array); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReferenceArray.html#getAndAccumulate(int, E, java.util.function.BinaryOperator) Comment More infoAdvertise with us Next Article AtomicReferenceArray getAndAccumulate() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-util-concurrent-atomic package Java-AtomicReferenceArray Practice Tags : Java Similar Reads AtomicReference getAndAccumulate() method in Java with Examples The getAndAccumulate() 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 previous value. The accumulatorFunction should be side-effect-free, s 2 min read AtomicReferenceArray getAndUpdate() method in Java with Examples The getAndUpdate() method of a AtomicReferenceArray class is used to atomically updates which updates the current value of the AtomicReferenceArray by applying the specified updateFunction operation on the current value. It takes an object of updateFunction interface as its parameter and applies the 2 min read AtomicLongArray getAndAccumulate() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.getAndAccumulate() is an inbuilt method in java that atomically updates the value at any index of the AtomicLongArray with the result after applying the given function to the current and given values, returning the previous value. This method accepts t 3 min read AtomicReferenceArray getAndSet() method in Java with Examples The getAndSet() method of a AtomicReferenceArray class is used to atomically sets the value of index i of AtomicReferenceArray object to newValue which is passed as parameter and returns the old value of the AtomicReferenceArray object, with memory effects as specified by VarHandle.getAndSet(java.la 2 min read AtomicReferenceArray getAcquire() method in Java with Examples The getAcquire() method of a AtomicReferenceArray class is used to return the value of the element at index i for this AtomicReferenceArray object with memory ordering effects compatible with memory_order_acquire ordering. Syntax: public final E getAcquire(int i) Parameters: This method accepts the 2 min read Like