AtomicReference accumulateAndGet() method in Java with Examples Last Updated : 03 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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, since it may be re-applied when attempted updates fail due to contention among threads. Syntax: public final E accumulateAndGet(E x, BinaryOperator<E> accumulatorFunction) Parameters: This method 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 updated value. Below programs illustrate the accumulateAndGet() method: Program 1: Java // Java program to demonstrate // AtomicReference.accumulateAndGet() method import java.util.concurrent.atomic.AtomicReference; import java.util.function.BinaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<Integer> ref = new AtomicReference<>(3456); // Print AtomicReference System.out.println( "The AtomicReference before update: " + ref); // Value to apply accumulateAndGet int x = 45654; // Declaring the accumulatorFunction // applying function to add value as string BinaryOperator add = (u, v) -> u.toString() + v.toString(); // apply accumulateAndGet() ref.accumulateAndGet(x, add); // print AtomicReference System.out.println( "The AtomicReference after update: " + ref); } } Output: Program 2: Java // Java program to demonstrate // AtomicReference.accumulateAndGet() method import java.util.concurrent.atomic.AtomicReference; import java.util.function.BinaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<String> ref = new AtomicReference<String>("GFG "); // Print AtomicReference System.out.println("The AtomicReference before update: " + ref); // Value to apply accumulateAndGet String x = "Welcome"; // Declaring the accumulatorFunction // applying function to add value as string BinaryOperator add = (u, v) -> v + " to " + u; // apply accumulateAndGet() ref.accumulateAndGet(x, add); // print AtomicReference System.out.println( "The AtomicReference after update: " + ref); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#accumulateAndGet(V, java.util.function.BinaryOperator) Comment More infoAdvertise with us Next Article AtomicLongArray accumulateAndGet() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Practice Tags : Java Similar Reads 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 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 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 AtomicLongArray accumulateAndGet() method in Java with Examples 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 3 min read AtomicLongArray accumulateAndGet() method in Java with Examples 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 3 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 Like