Skip to content
Related Articles

Related Articles

AtomicInteger accumulateAndGet() method in Java with Examples

View Discussion
Improve Article
Save Article
  • Last Updated : 04 Feb, 2021
View Discussion
Improve Article
Save Article

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 the operation specified in the object to the values. It returns the updated value.


public final int accumulateAndGet(int y, 
             IntBinaryOperator function)

Parameters: This method accepts as parameter an integer value y and an IntBinaryOperator function. It applies the given function to the current value of the object and the value y.

Return Value: The function returns the updated value of the current object.

Example to demonstrate the function. 


// Java program to demonstrate
// AtomicInteger accumulateAndGet() method
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntBinaryOperator;
public class Demo {
    public static void main(String[] args)
        new UserThread("Thread A");
        new UserThread("Thread B");
class Shared {
    static AtomicInteger ai = new AtomicInteger(0);
class UserThread implements Runnable {
    String name;
    UserThread(String name)
    { = name;
        new Thread(this).start();
    IntBinaryOperator ibo = (x, y) -> (x + y);
    int value = 5;
    public void run()
        for (int i = 0; i < 3; i++) {
            int ans =
                          .accumulateAndGet(value, ibo);
            System.out.println(name + " " + ans);


Thread A 5
Thread A 10
Thread A 15
Thread B 20
Thread B 25
Thread B 30



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!