AtomicLong updateAndGet() method in Java with Examples
The Java.AtomicLong.updateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value. It takes an object of LongUnaryOperator interface as its parameter and applies the operation specified in the object to the current value. It returns the updated value.
Syntax:
public final long updateAndGet(LongUnaryOperator function)
Parameters: This method accepts as parameter an LongUnaryOperator function. It applies the given function to the current value of the object.
Return Value: The function returns the updated value of the current object.
Example to demonstrate the function.
// Java programt to demonstrate // AtomicLong updateAndGet() method import java.util.concurrent.atomic.AtomicLong; import java.util.function.LongUnaryOperator; public class Demo { public static void main(String[] args) { // AtomicLong initialized with a value of -20000 AtomicLong al = new AtomicLong(- 20000 ); // Unary operator defined to negate the value LongUnaryOperator unaryOperator = (x) -> - x; System.out.println( "Initial Value is " + al); // Function called and the unary operator // is passed as an argument long x = al.updateAndGet(unaryOperator); System.out.println( "Updated value is " + x); } } |
Initial Value is -20000 Updated value is 20000
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicLong.html
Recommended Posts:
- AtomicLongArray updateAndGet() method in Java with Examples
- AtomicIntegerArray updateAndGet() method in Java with Examples
- AtomicInteger updateAndGet() method in Java with Examples
- AtomicLong set() method in Java with examples
- AtomicLong get() method in Java with examples
- AtomicLong decrementAndGet() method in Java with examples
- AtomicLong addAndGet() method in Java with examples
- AtomicLong lazySet() method in Java with examples
- AtomicLong getAndDecrement() method in Java with examples
- AtomicLong getAndSet() method in Java with examples
- AtomicLong getAndUpdate() method in Java with Examples
- AtomicLong accumulateAndGet() method in Java with Examples
- AtomicLong toString() method in Java with examples
- AtomicLong intValue() method in Java with examples
- AtomicLong weakCompareAndSet() 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.