The AtomicLong class provides you with a long variable which can be read and written atomically, and which also contains advanced atomic operations like compareAndSet(). The AtomicLong class is located in the java.util.concurrent.atomic package, so the full class name is java.util.concurrent.atomic.AtomicLong . This text describes the version of AtomicLong found in Java 8, but the first version was added in Java 5.

The reasoning behind the AtomicLong design is explained in my Java Concurrency tutorial in the text about Compare and Swap.

Creating an AtomicLong

Creating an AtomicLong is done like this:

AtomicLong atomicLong = new AtomicLong();

This example creates an AtomicLong with the initial value 0 .

If you want to create an AtomicLong with an initial value, you can do so like this:

AtomicLong atomicLong = new AtomicLong(123);

This example passes a value of 123 as parameter to the AtomicLong contructor, which sets the initial value of the AtomicLong instance to 123 .

Getting the AtomicLong Value

You can get the value of an AtomicLong instance via the get() method. Here is an AtomicLong.get() example:

AtomicLong atomicLong = new AtomicLong(123);

long theValue = atomicLong.get();

Setting the AtomicLong Value

You can set the value of an AtomicLong instance via the set() method. Here is an AtomicLong.set() example:

AtomicLong atomicLong = new AtomicLong(123);


This example creates an AtomicLong example with an initial value of 123, and then sets its value to 234 in the next line.

Compare and Set the AtomicLong Value

The AtomicLong class also has an atomic compareAndSet() method. This method compares the current value of the AtomicLong instance to an expected value, and if the two values are equal, sets a new value for the AtomicLong instance. Here is an AtomicLong.compareAndSet() example:

AtomicLong atomicLong = new AtomicLong(123);

long expectedValue = 123;
long newValue      = 234;
atomicLong.compareAndSet(expectedValue, newValue);

This example first creates an AtomicLong instance with an initial value of 123 . Then it compares the value of the AtomicLong to the expected value 123 and if they are equal the new value of the AtomicLong becomes 234;

Adding to the AtomicLong Value

The AtomicLong class contains a few methods you can use to add a value to the AtomicLong and get its value returned. These methods are:

  • addAndGet()
  • getAndAdd()
  • getAndIncrement()
  • incrementAndGet()

The first method, addAndGet() adds a number to the AtomicLong and returns its value after the addition. The second method, getAndAdd() also adds a number to the AtomicLong but returns the value the AtomicLong had before the value was added. Which of these two methods you should use depends on your use case. Here are two examples:

AtomicLong atomicLong = new AtomicLong();


This example will print out the values 0 and 20. First the example gets the value of the AtomicLong before adding 10 to. Its value before addition is 0. Then the example adds 10 to the AtomicLong and gets the value after the addition. The value is now 20.

You can also add negative numbers to the AtomicLong via these two methods. The result is effectively a subtraction.

The methods getAndIncrement() and incrementAndGet() works like getAndAdd() and addAndGet() but just add 1 to the value of the AtomicLong.

Subtracting From the AtomicLong Value

The AtomicLong class also contains a few methods for subtracting values from the AtomicLong value atomically. These methods are:

  • decrementAndGet()
  • getAndDecrement()

The decrementAndGet() subtracts 1 from the AtomicLong value and returns its value after the subtraction. The getAndDecrement() also subtracts 1 from the AtomicLong value but returns the value the AtomicLong had before the subtraction.

Using an AtomicLong as a Counter in a Lambda Expression

Java Lambda Expression cannot contain any member fields, and thus they cannot keep any state internally between calls to the lambda expression. However, you can bypass this limitation by creating an AtomicLong outside the Lambda Expression and use it from inside the Lambda Expression. Here is an example of that:

import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;

public class AtomicLongExample {

    public static void main(String[] args) {

        AtomicLong atomicLong = new AtomicLong();

        Function<Long, Long> myLambda = (input) -> {
            long noOfCalls = atomicLong.incrementAndGet();
            System.out.println("Lambda called " + noOfCalls + " times.");
            return input * 2;


The output from running the above code look like this:

Lambda called 1 times.
Lambda called 2 times.
Lambda called 3 times.

Leave a Comment