Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaAtomicLongArray getAndAccumulate() method in Java with Examples

AtomicLongArray getAndAccumulate() method in Java with Examples

The Java.util.concurrent.atomic.AtomicLongArray.getAndAccumulate() is an inbuilt method in java that atomically updates the value at any index of the AtomicLongArray with the result after applying the given function to the current and given values, returning the previous value. This method accepts the index of the AtomicLongArray where the operation is to be done, the value with which operation is performed and the accumulator function as the parameter. The function is applied with the current value at index as its first argument, and the given update as the second argument. The accumulator function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function getAndAccumulate() is similar to accumulateAndGet() but the former function returns the value before update whereas the latter returns value after the update.

Syntax:

public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulatorFunction)

Parameters: The function accepts three parameters:

  • i – The index where update is to be made.
  • x – The value to make operation with value at i
  • accumulatorFunction – A side-effect-free function of two arguments.
  • Return value: The function returns the value before the update which is in long.
    Below programs illustrate the above method:
    Program 1:




    // Java program that demonstrates
    // the getAndAccumulate() function
      
    import java.util.concurrent.atomic.AtomicLongArray;
    import java.util.function.LongBinaryOperator;
      
    public class GFG {
        public static void main(String args[])
        {
            // Initializing an array
            long a[] = { 1, 2, 3, 4, 5 };
      
            // Initializing an AtomicLongArray with array a
            AtomicLongArray arr = new AtomicLongArray(a);
      
            // Displaying the AtomicLongArray
            System.out.println("The array : " + arr);
      
            // Index where update is to be made
            int idx = 4;
      
            // Value to make operation with value at idx
            long x = 5;
      
            // Declaring the accumulatorFunction
            LongBinaryOperator add = (u, v) -> u + v;
      
            // Updating the value at idx
            // applying getAndAccumulate
            long prev = arr.getAndAccumulate(idx, x, add);
      
            // The previous value at idx
            System.out.println("Value at index " + idx
                               + " before update is "
                               + prev);
      
            // Displaying the AtomicLongArray
            System.out.println("The array after update : "
                               + arr);
        }
    }

    
    
    Output:

    The array : [1, 2, 3, 4, 5]
    Value at index 4 before update is 5
    The array after update : [1, 2, 3, 4, 10]
    

    Program 2:




    // Java program that demonstrates
    // the getAndAccumulate() function
      
    import java.util.concurrent.atomic.AtomicLongArray;
    import java.util.function.LongBinaryOperator;
      
    public class GFG {
        public static void main(String args[])
        {
            // Initializing an array
            long a[] = { 1, 2, 3, 4, 5 };
      
            // Initializing an AtomicLongArray with array a
            AtomicLongArray arr = new AtomicLongArray(a);
      
            // Displaying the AtomicLongArray
            System.out.println("The array : " + arr);
      
            // Index where update is to be made
            int idx = 0;
      
            // Value to make operation with value at idx
            long x = 6;
      
            // Declaring the accumulatorFunction
            LongBinaryOperator sub = (u, v) -> u - v;
      
            // Updating the value at idx
            // applying getAndAccumulate
            long prev = arr.getAndAccumulate(idx, x, sub);
      
            // The previous value at idx
            System.out.println("Value at index " + idx
                               + " before update is "
                               + prev);
      
            // Displaying the AtomicLongArray
            System.out.println("The array after update : "
                               + arr);
        }
    }

    
    
    Output:

    The array : [1, 2, 3, 4, 5]
    Value at index 0 before update is 1
    The array after update : [-5, 2, 3, 4, 5]
    

    Reference:
    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLongArray.html#getAndAccumulate-int-long-java.util.function.LongBinaryOperator-

    RELATED ARTICLES

    Most Popular

    Recent Comments