AtomicReference getAndUpdate() method in Java with Examples Last Updated : 15 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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 specified in the object to the current value. It returns the previous value.Syntax: public final V getAndUpdate(UnaryOperator<V> updateFunction) Parameters: This method accepts updateFunction which is a side-effect-free function.Return value: This method returns the previous value.Below programs illustrate the getAndUpdate() method: Program 1: Java // Java program to demonstrate // AtomicReference.getAndUpdate() method import java.util.concurrent.atomic.AtomicReference; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<Integer> ref = new AtomicReference<>(987654); // Declaring the updateFunction // applying function UnaryOperator twoDigits = (v) -> v.toString() .substring(0, 2); // apply getAndUpdate() int value = ref.getAndUpdate(twoDigits); // 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.getAndUpdate() method import java.util.concurrent.atomic.*; import java.util.function.UnaryOperator; public class GFG { public static void main(String args[]) { // AtomicReference with value AtomicReference<String> ref = new AtomicReference<>("welcome"); // Declaring the updateFunction // applying function UnaryOperator twoDigits = (v) -> v + " to gfg"; // apply getAndUpdate() String value = ref.getAndUpdate(twoDigits); // print AtomicReference System.out.println( "The AtomicReference previous value: " + value); System.out.println( "The AtomicReference new value: " + ref.get()); } } Output: References: https://docs.oracle.com/javase/10/docs/api/java/util/concurrent/atomic/AtomicReference.html#getAndUpdate(java.util.function.UnaryOperator) Comment More infoAdvertise with us Next Article AtomicReference getAndUpdate() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Practice Tags : Java Similar Reads 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 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 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 AtomicReference getOpaque() method in Java with Examples The getOpaque() method of a AtomicReference class is used to return the current value of AtomicReference object, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...). This VarHandle.getOpaque(java.lang.Object...) method handles operation with no assurance of memory ordering e 1 min read AtomicReference get() method in Java with Examples The get() method of a AtomicReference class is used to return the value of this AtomicReference object with memory semantics of reading as if the variable was declared volatile type of variable. Syntax: public final V get() Parameters: This method accepts nothing. Return value: This method returns c 1 min read Like