Java - How to use AtomicReference

AtomicReference is a class wrapping an object of class V (Generic). The AtomicReference class guarantees concurrency in a multithreaded environment.

There are 3 ways to solve concurrency problem in Java.

  • "volatile" guarantees concurrency only when writing from Thread1 and reading from Thread2. This can be a problem if you write in two threads.
  • By using "synchronized" you can safely guarantee concurrency. but the most expensive
  • Atomic classes use compare-and-swap to ensure concurrency. There is no problem with writing data from multiple threads

AtomicReference can guarantee concurrency at a lower cost than synchronized .

Let`s see how to use AtomicReference.

create object

AtomicReference is implemented as a Generic class and can be used with various classes.

Let`s create as an example.

You can pass an initial value to the constructor, and if not entered, the object is initialized to null.

public void atomicReference1() {
    AtomicReference<Integer> atomic = new AtomicReference<>();
    System.out.println("atomic : " + atomic.get());

    AtomicReference<Integer> atomic2 = new AtomicReference<>(10);
    System.out.println("atomic2 : " + atomic2.get());
}

result

atomic : null
atomic2 : 10

get(), set(), getAndSet()

You can use set(V newValue) method to set the value of AtomicReference, and get() method to read the value.

public void atomicReference2() {
    AtomicReference<Integer> atomic = new AtomicReference<>();
    System.out.println("value : " + atomic.get());

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

result

value : null
value : 100

getAndSet(V newValue) returns the current value and updates it with the value passed as an argument.

public void atomicReference3() {
    AtomicReference<Integer> atomic = new AtomicReference<>(10);
    System.out.println("value(before setting) : " + atomic.getAndSet(20));
    System.out.println("value(after setting) : " + atomic.get());
}

result

value(before setting) : 10
value(after setting) : 20

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(V expect, V update)

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

public void atomicReference4() {
    int expected = 20;
    AtomicReference<Integer> atomic = new AtomicReference<>(10);
    System.out.println("success ? " + atomic.compareAndSet(expected, 100));
    System.out.println("value : " + atomic.get());

    atomic.set(20);
    System.out.println("success ? " + atomic.compareAndSet(expected, 100));
    System.out.println("value : " + atomic.get());
}

result

success ? false
value : 10
success ? true
value : 100

Reference

codechachaCopyright ©2019 codechacha