AtomicLongArray getAndAccumulate() method in Java, examples
Original text: https://www . geeksforgeeks . org/atomicongarray-getandaccumulate-method-in-Java-with-examples/
Java . util . concurrent . atomic . AtomicLongArray . getandaccumulate() is a built-in method in Java that applies the given function to the current value and the given value, automatically updates the value at any index of the atomicongarray with the result, returning the previous value. This method accepts as parameters the index of the atomic clone array to perform the operation on, the value to perform the operation on, and the accumulator function. This function is applied with the current value at index as its first argument and the given update as its second argument. The accumulator function should have no side effects because it can be reapplied when an attempted update fails due to contention between threads. The function getAndAccumulate() is similar to accumulated gt(), but the former returns a value before updating and the latter returns a value after updating.
Syntax:
Public final long getAndAccumulate(int i, long x, LongBinaryOperator accumulator function)
Parameters:This function accepts three parameters:
-
I – the index to be updated. x–The value to operate on with the value at I accumulatorFunction – A side-effect-free function of two arguments.
Return value: This function returns the value before update, which is in long.
The following program illustrates the above method:
Program 1:“`java
// Java program that demonstrates
// the getAndAccumulate( ) functionimport 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; 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:
“`java
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
// Java program that demonstrates
// the getAndAccumulate() functionimport 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
. / Displaying the AtomicLongArray
System.out.println(“The array after update: “
>Output:“`java
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/atomicongarray . html # getAndAccumulate-int-long-Java . util . function . longbinaryoperator-