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-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 E accumulateAndGet(int i, E x, BinaryOperator<E> accumulatorFunction)
Parameters: This method accepts:
- i which is an index of AtomicReferenceArray to perform the operation 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:
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.