CAS
Javaμμμ CAS
public static void main(String[] args) {
AtomicInteger atomicInt = new AtomicInteger(0);
int expectedValue = 0;
int newValue = 1;
// νμ¬ κ°μ΄ 0(expectedValue)μ΄λ©΄ 1(newValue)λ‘ κ΅μ²΄
boolean success = atomicInt.compareAndSet(expectedValue, newValue);
System.out.println("CAS μ±κ³΅ μ¬λΆ: " + success); // true
System.out.println("νμ¬ κ°: " + atomicInt.get()); // 1
}Atomicκ³Ό volatile
vs λ½ κΈ°λ° λκΈ°ν
νΉμ§
λ½(Lock) λ°©μ
CAS(Compare-And-Swap) λ°©μ
Last updated