The java.util.concurrent.atomic.AtomicInteger.addandget() is an inbuilt method in java which adds the value which is passed in the parameter of the function to the previous value and returns the new updated value which is of data-type int. Syntax:
public final int addAndGet(int val)
Parameters: The function accepts a single mandatory parameter val which specifies the value to be added. Return value: The function returns the integer value after addition is done. Program below demonstrates the function: Program 1:Â
Java
// Java Program to demonstrates // the addandget() function Â
import java.util.concurrent.atomic.AtomicInteger; Â
public class GFG { Â Â Â Â public static void main(String args[]) Â Â Â Â { Â
        // Initially value as 0         AtomicInteger val             = new AtomicInteger(); Â
        // Update the value         int c = val.addAndGet( 6 ); Â
        // Prints the updated value         System.out.println("Updated value: "                            + c);     } } |
Updated value: 6
Program 2:Â
Java
// Java Program to demonstrates // the addandget() function Â
import java.util.concurrent.atomic.AtomicInteger; public class GFG { Â Â Â Â public static void main(String args[]) Â Â Â Â { Â
        // Initially value as 18         AtomicInteger val             = new AtomicInteger( 18 ); Â
        // Prints the updated value         System.out.println("Previous value: "                            + val); Â
        // adds the value to 18         val.addAndGet( 6 ); Â
        // Prints the updated value         System.out.println("Updated value: "                            + val);     } } |
Previous value: 18 Updated value: 24
Program 3:
Java
/*package whatever //do not write package name here */ Â
import java.io.*; import java.util.concurrent.atomic.AtomicInteger; Â
class GFG {     public static void main(String[] args) throws InterruptedException     {         AtomicInteger count = new AtomicInteger( 0 ); Â
        Thread thread1 = new Thread(() -> {             for ( int i = 0 ; i < 1000 ; i++) {                 count.incrementAndGet();             }         }); Â
        Thread thread2 = new Thread(() -> {             for ( int i = 0 ; i < 1000 ; i++) {                 count.incrementAndGet();             }         }); Â
        thread1.start();         thread2.start(); Â
        thread1.join();         thread2.join(); Â
        System.out.println( "Final count: " + count.get());     } } |
Final count: 2000