AtomicReference getAndAccumulate() method in Java with Examples Last Updated : 15 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.Syntax: public final E getAndAccumulate(E x, BinaryOperator<E> accumulatorFunction) Parameters: This method accepts: x which is the updated value andaccumulatorFunction 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 // AtomicReference.getAndAccumulate() method import java.util.concurrent.atomic.*; import java.util.function.BinaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<Integer> ref = new AtomicReference<>(3456); // Value to apply getAndAccumulate int x = 45654; // Declaring the accumulatorFunction // applying function to add value as string BinaryOperator add = (u, v) -> u.toString() + v.toString(); // apply getAndAccumulate() int value = ref.getAndAccumulate(x, add); // print AtomicReference System.out.println( "The AtomicReference previous value: " + value); System.out.println( "The AtomicReference new value: " + ref.get()); } } Output: Program 2: Java // Java program to demonstrate // AtomicReference.getAndAccumulate() method import java.util.concurrent.atomic.*; import java.util.function.BinaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<String> ref = new AtomicReference<String>("GFG "); // Value to apply getAndAccumulate String x = "Welcome"; // Declaring the accumulatorFunction // applying function to add value as string BinaryOperator add = (u, v) -> v + " to " + u; // apply getAndAccumulate() String previousValue = ref.getAndAccumulate(x, add); // print AtomicReference System.out.println( "The AtomicReference previous value: " + previousValue); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#getAndAccumulate(V, java.util.function.BinaryOperator) Comment More infoAdvertise with us Next Article AtomicReference getAndAccumulate() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Practice Tags : Java Similar Reads AtomicReferenceArray getAndAccumulate() method in Java with Examples 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 2 min read AtomicReference getAndUpdate() method in Java with Examples The getAndUpdate() method of a AtomicReference class is used to atomically updates which updates the current value of the AtomicReference by applying the specified updateFunction operation on the current value. It takes an object of updateFunction interface as its parameter and applies the operation 2 min read AtomicInteger getAndAccumulate() method in Java with Examples The Java.AtomicInteger.getAndAccumulate() 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 AtomicReference getAcquire() method in Java with Examples The getAcquire() method of a AtomicReference class is used to return the value of this AtomicReference object with memory ordering effects on getting variable is compatible with memory_order_acquire ordering. Syntax: public final V getAcquire() Parameters: This method accepts nothing.Return value: T 1 min read AtomicReference getAndSet() method in Java with Examples The getAndSet() method of a AtomicReference class is used to atomically sets the value of AtomicReference object to newValue which is passed as parameter and returns the old value of the AtomicReference object, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...).VarHandle.ge 2 min read Like