Java - How to use AtomicLong

AtomicLong is a Wrapping class with Long data type.

It is implemented as thread-safe and can be used without synchronization in multi-threads. It also guarantees concurrency at a lower cost than synchronized .

Let`s see how to use the AtomicLong class.

create object

AtomicLong can be created like this:

  • AtomicLong() : creates an AtomicLong with an initial value of 0
  • AtomicLong(longVal) : creates an AtomicLong initialized with the value of the argument
AtomicLong atomic = new AtomicLong();
System.out.println("AtomicLong() : " + atomic.get());

AtomicLong atomic2 = new AtomicLong(10);
System.out.println("AtomicLong(10) : " + atomic2.get());

Output:

AtomicLong() : 0
AtomicLong(10) : 10

get(), set(), getAndSet()

You must use the set(newValue) method to change the value of AtomicLong, and the get() method to read the value. getAndSet(newValue) returns the currently set value and updates it with the new value.

AtomicLong atomic = new AtomicLong();

atomic.set(100);
System.out.println("get() : " + atomic.get());

System.out.println("getAndSet(200) : " + atomic.getAndSet(200));
System.out.println("get() : " + atomic.get());

Output:

get() : 100
getAndSet(200) : 100
get() : 200

getAndUpdate(), updateAndGet()

getAndUpdate(LongUnaryOperator) is a bit different from getAndSet(). You can pass a lambda expression as the second argument, so the function can change its value.

updateAndGet(LongUnaryOperator) returns the set value after update.

AtomicLong atomic = new AtomicLong(10);
LongUnaryOperator square = (n) -> n * n;
System.out.println("getAndUpdate(square) : " + atomic.getAndUpdate(square));
System.out.println("get() : " + atomic.get());

AtomicLong atomic2 = new AtomicLong(5);
System.out.println("updateAndGet(square) : " + atomic2.updateAndGet(square));
System.out.println("get() : " + atomic2.get());

Output:

getAndUpdate(square) : 10
get() : 100
updateAndGet(square) : 25
get() : 25

getAndIncrement(), getAndAdd()

  • getAndIncrement() : return current value and increment by +1
  • incrementAndGet() : increment by +1 and return the changed value
  • getAndDecrement() : return current value and decrement -1
  • decrementAndGet() : decrement by -1 and return the changed value
  • getAndAdd(newValue) : Returns the current value and adds newValue to the current value.
  • addAndGet(newValue) : Adds newValue to the current value and returns the result.
AtomicLong atomic = new AtomicLong(100);

System.out.println("getAndIncrement() : " + atomic.getAndIncrement());
System.out.println("get() : " + atomic.get());

System.out.println("incrementAndGet() : " + atomic.incrementAndGet());

System.out.println("decrementAndGet() : " + atomic.decrementAndGet());

System.out.println("getAndDecrement() : " + atomic.getAndDecrement());
System.out.println("get() : " + atomic.get());

System.out.println("addAndGet(300) : " + atomic.addAndGet(300));

System.out.println("getAndAdd(400) : " + atomic.getAndAdd(400));
System.out.println("get() : " + atomic.get());

result

getAndIncrement() : 100
get() : 101
incrementAndGet() : 102
decrementAndGet() : 101
getAndDecrement() : 101
get() : 100
addAndGet(300) : 400
getAndAdd(400) : 400
get() : 800

compareAndSet()

compareAndSet(expect, update) returns true if the current value is the same as the expected value (expect). Otherwise, there is no data change and returns false.

/**
 * Atomically sets the value to the given updated value
 * if the current value {@code ==} the expected value.
 *
 * @param expect the expected value
 * @param update the new value
 * @return {@code true} if successful. False return indicates that
 * the actual value was not equal to the expected value.
 */
public final boolean compareAndSet(long expect, long update)

Here is an example using compareAndSet(). The data is updated only when it matches the current value and true is returned.

AtomicLong atomic = new AtomicLong(100);

int expected = 10;
int update = 1000;

System.out.println("success ? " + atomic.compareAndSet(expected, update));
System.out.println("get() : " + atomic.get());

expected = 100;
System.out.println("success ? " + atomic.compareAndSet(expected, update));
System.out.println("get() : " + atomic.get());

Output:

success ? false
get() : 100
success ? true
get() : 1000

Reference

codechachaCopyright ©2019 codechacha