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.
Syntax:
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:
Program 1:
// Java program that demonstrates // the updateAndGet() function import java.util.concurrent.atomic.AtomicLongArray; import java.util.function.LongUnaryOperator; 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 ; // Declaring the updateFunction LongUnaryOperator squaredFunction = (l) -> l * l; // Updating the value at idx // applying updateFunction arr.updateAndGet(idx, squaredFunction); // Displaying the AtomicLongArray System.out.println( "The array after update : " + arr); } } |
The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 4, 25]
Program 2:
// Java program that demonstrates // the updateAndGet() function import java.util.concurrent.atomic.AtomicLongArray; import java.util.function.LongUnaryOperator; 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 = 3 ; // Declaring the updateFunction LongUnaryOperator cubeFunction = (l) -> l * l * l; // Updating the value at idx // applying updateFunction arr.updateAndGet(idx, cubeFunction); // Displaying the AtomicLongArray System.out.println( "The array after update : " + arr); } } |
The array : [1, 2, 3, 4, 5] The array after update : [1, 2, 3, 64, 5]
Recommended Posts:
- AtomicLong updateAndGet() method in Java with Examples
- AtomicInteger updateAndGet() method in Java with Examples
- AtomicIntegerArray updateAndGet() method in Java with Examples
- AtomicLongArray get() method in Java with Examples
- AtomicLongArray set() method in Java with Examples
- AtomicLongArray getAndAdd() method in Java with Examples
- AtomicLongArray getAndSet() method in Java with Examples
- AtomicLongArray getAndIncrement() method in Java with Examples
- AtomicLongArray length() method in Java with Examples
- AtomicLongArray incrementAndGet() method in Java with Examples
- AtomicLongArray getAndDecrement() method in Java with Examples
- AtomicLongArray getAndAccumulate() method in Java with Examples
- AtomicLongArray getAndUpdate() method in Java with Examples
- AtomicLongArray toString() method in Java with Examples
- AtomicLongArray lazySet() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.