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 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.
public final long accumulateAndGet(int i, long x, LongBinaryOperator accumulatorFunction)
Parameters: The function accepts three parameters:
Return value: The function returns the updated value which is in long.
Below programs illustrate the above method:
The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 4, 10]
The array : [17, 22, 33, 44, 55] The array after update : [11, 22, 33, 44, 55]
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.