# 자바 동시성 프로그래밍 - Java 동기화 도구

## AtomicFieldUpdater

* 지정된 클래스의 `volatile` 필드에 대한 원자적 업데이트를 가능하게 하는 리플렉션 기반 유틸리티이다.
* 주로 클래스 내부의 필드를 원자적으로 변경하는 경우에 사용된다.

### 클래스

![img\_20.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-8acaa539919b1e3dad8b0dbd17c08cdfb231bee4%2Fimg_20.png?alt=media)

### 생성

![img\_21.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-338bd0ac71239519bcfb95339edb97ef1a90bee9%2Fimg_21.png?alt=media)

### 공통 API

![img\_22.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-2fcb245fd7f7896c6d3152935bd125cec40b14d9%2Fimg_22.png?alt=media)

***

## AtomicIntegerFieldUpdater 기본 구현

![img\_23.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-9f81f755e0f481658374a1e47bec72a7d78a4962%2Fimg_23.png?alt=media)

***

## AtomicFieldUpdater vs AtomicVariable

![img\_24.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-d21a68d0d89409ba53e7b52efdf6f2be8ef3a22d%2Fimg_24.png?alt=media)

***

## AtomicFieldUpdater 예제 코드

### 1

![img\_25.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-95283402096bbd70387a5fb2f88f05f7ee2d5b6b%2Fimg_25.png?alt=media)

### 2

![img\_26.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-3e431858dabbddffb4413c803931bb29a7f85ace%2Fimg_26.png?alt=media)

![img\_27.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-80bf6342dd451783a5e971c7514b705ca297beb5%2Fimg_27.png?alt=media)

* `AtomicFieldUpdater`는 리플렉션 기반이기 때문에 모든 연산은 인스턴스를 인자로 넘겨주어야 한다.

### 3

![img\_28.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-ede5e99b2874100deb5bfdce9d15027b944bc0d1%2Fimg_28.png?alt=media)

![img\_29.png](https://2900387690-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOjEy54f4ToLNl5n9gmZs%2Fuploads%2Fgit-blob-044aa3f2fd58f47bba876a4df141872d22d946d8%2Fimg_29.png?alt=media)

* **CAS** 연산으로 두 스레드의 동시 접근을 막을 수 있다.

***

[이전 ↩️ - Java 동기화 도구 - Atomic Variable](https://github.com/genesis12345678/TIL/blob/main/Java/reactive/javaSync/Atomic.md)

[메인 ⏫](https://github.com/genesis12345678/TIL/blob/main/Java/reactive/Main.md)

[다음 ↪️ - Java 동기화 도구 - CountDownLatch](https://github.com/genesis12345678/TIL/blob/main/Java/reactive/javaSync/CountDownLatch.md)
