AtomicLongArray updateAndGet() method in Java with Examples
The Java.util.concurrent.atomic.AtomicLongArray.updateAndGet() is an inbuilt method in Java that updates the value at any given index of the AtomicLongArray after applying a given update function on the value at that index. The method takes the index value of the AtomicLongArray and the update function as the parameters and updates the value at that index by applying the update function on that value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
public final long updateAndGet(int i, LongUnaryOperator updateFunction)
Parameters: The function accepts two parameters:
Return value: The function returns a long value which is the value after applying the specified update function.
Below programs illustrate the above method:
The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 4, 25]
The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 64, 5]