AtomicInteger addAndGet() method in Java with examples Last Updated : 27 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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); } } Output: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); } } Output: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()); } } OutputFinal count: 2000 Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html#addAndGet-int- Comment More infoAdvertise with us Next Article AtomicInteger addAndGet() method in Java with examples gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions Java-AtomicInteger Practice Tags : Java Similar Reads AtomicIntegerArray addAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.addAndGet() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicIntegerArray. This method takes the index value and the value to be added as the parameters and returns the updated value at this i 2 min read AtomicLong addAndGet() method in Java with examples The Java.util.concurrent.atomic.AtomicLong.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 long. Syntax: public final long addAndGet(long val) Parameters: The 2 min read AtomicLongArray addAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicLongArray.addAndGet() is an inbuilt method in Java that atomically adds the given value to the element at an index of the AtomicLongArray. This method takes the index value and the value to be added as the parameters and returns the updated value at this index. 2 min read AtomicInteger accumulateAndGet() method in Java with Examples The Java.AtomicInteger.accumulateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value and value passed as a parameter. It takes an integer as its parameter and an object of IntBinaryOperator interface and applies 2 min read AtomicInteger get() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.get() is an inbuilt method in java which returns the current value which is of date-type int. Syntax: public final int get() Parameters: The function does not accepts any parameter. Return value: The function returns the current value Program below demon 1 min read AtomicIntegerArray accumulateAndGet() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.accumulateAndGet() is an inbuilt method in java that atomically updates the element at index i with the results of applying the given function to the current and given values, returning the updated value. The function should be side-effect-free, sin 3 min read AtomicInteger set() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.set() is an inbuilt method in java that updates the previous value and sets it to a new value which is passed in the parameter. Syntax: public final void set(int newVal) Parameters: The function accepts a single mandatory parameter newVal which is to be 1 min read AtomicLong accumulateAndGet() method in Java with Examples The Java.AtomicLong.accumulateAndGet() method is an inbuilt method, which updates the current value of the object by applying the specified operation on the current value and value passed as a parameter. It takes a long as its parameter and an object of LongBinaryOperator interface and applies the o 2 min read AtomicIntegerArray get() method in Java with Examples The Java.util.concurrent.atomic.AtomicIntegerArray.get() is an inbuilt method in java that gets the current value at any position of the AtomicIntegerArray. This method takes the index value as the parameter and returns the value at this index. Syntax: public final int get(int i) Parameters: The fun 2 min read AtomicInteger intValue() method in Java with examples The java.util.concurrent.atomic.AtomicInteger.intValue() is an inbuilt method in java that returns the value which is currently stored in the object which is of data-type int. Syntax: public int intValue() Parameters: The function does not accepts a single parameter. Return value: The function retur 1 min read Like