CAS (Compare and Set)
CAS - 동기화와 원자적 연산
1. CAS (Compare and Set)
package thread.cas;
import java.util.concurrent.atomic.AtomicInteger;
public class CasMainV1 {
public static void main(String[] args) {
AtomicInteger atomicInteger = new AtomicInteger(0);
System.out.println("start value = " + atomicInteger.get());
boolean result1 = atomicInteger.compareAndSet(0, 1);
System.out.println("result1 = " + result1 + ", value = " + atomicInteger.get());
boolean result2 = atomicInteger.compareAndSet(0, 1);
System.out.println("result2 = " + result2 + ", value = " + atomicInteger.get());
}
}
1.1. 멀티스레드 상황
2. Lock vs CAS
2.1. Lock
2.2. CAS
3. CAS 구현
3.1. 락 구현1 (bad)
3.2. 락 구현2 (good)
4. CAS 단점
Last updated