You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
1. CAS 연산 1 (Compare-And-Swap)
락(Lock) 기반 방식의 문제점
비관적(Pessimistic) 접근
오버헤드
CAS 연산이란?
정의
낙관적(Optimistic) 접근
동작 원리 (
compareAndSet)true를 반환함.false를 반환함.하드웨어의 지원
2. CAS 연산 2 - 원자적 연산 직접 구현
AtomicInteger가 제공하는incrementAndGet()메서드의 내부 로직을 CAS 연산을 활용해 직접 구현한 예시임.CasMainV2 클래스
3. CAS 연산 3 - 멀티스레드 충돌 분석
getValue = 0을 읽음compareAndSet(0, 1)에 성공하여 메모리 값을 1로 변경함compareAndSet(0, 1)을 시도하지만, 메모리 값이 이미 1이므로 **실패(false)**함do-while루프를 다시 돌아 현재 값 1을 읽고,compareAndSet(1, 2)를 시도하여 성공함CAS 방식의 장단점
4. 정리: CAS vs 동기화 락(Lock)
애플리케이션의 상황에 맞는 적절한 동기화 전략 선택이 중요함.
그래서 언제 무엇을 사용해야하냐면?
CAS를 사용해야 하는 경우
동기화 락을 사용해야 하는 경우
Beta Was this translation helpful? Give feedback.
All reactions