Java - AtomicReference 사용 방법

AtomicReference는 V 클래스(Generic)의 객체를 wrapping 클래스입니다. AtomicReference 클래스는 멀티쓰레드 환경에서 동시성을 보장합니다.

자바에서 동시성 문제를 해결하는데 3가지 방법이 있습니다.

  • "volatile" 은 Thread1에서 쓰고, Thread2에서 읽는 경우만 동시성을 보장합니다. 두개의 쓰레드에서 쓴다면 문제가 될 수 있습니다.
  • "synchronized"를 쓰면 안전하게 동시성을 보장할 수 있습니다. 하지만 비용이 가장 큽니다.
  • Atomic 클래스는 CAS(compare-and-swap)를 이용하여 동시성을 보장합니다. 여러 쓰레드에서 데이터를 write해도 문제가 없습니다.

AtomicReference는 synchronized 보다 적은 비용으로 동시성을 보장할 수 있습니다.

AtomicReference를 사용하는 방법에 대해서 알아보겠습니다.

객체 생성

AtomicReference는 Generic class로 구현되어, 다양한 클래스와 함께 사용할 수 있습니다.

Integer 객체를 갖는 AtomicReference<Integer>를 예제로 생성해보겠습니다.

생성자에 초기값을 전달할 수 있으며, 입력하지 않으면 객체는 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());
}

결과

atomic : null
atomic2 : 10

get(), set(), getAndSet()

AtomicReference의 값을 set(V newValue) 메소드를, 값을 읽으려면 get() 메소드를 사용하면 됩니다.

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

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

결과

value : null
value : 100

getAndSet(V newValue)은 현재의 value를 리턴하고, 인자로 전달된 값으로 업데이트합니다.

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());
}

결과

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

compareAndSet()

compareAndSet(expect, update)는 현재 값이 예상하는 값(expect)과 동일하다면 update 값으로 변경해주고 true를 리턴해 줍니다. 그렇지 않다면 데이터 변경은 없고 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)

다음은 compareAndSet()를 사용하는 예제입니다. 현재 값과 일치할 때만 데이터가 업데이트되고 true가 리턴됩니다.

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());
}

결과

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

참고

Loading script...

Related Posts

codechachaCopyright ©2019 codechacha