compare-and-swap
์ผ๋ช CAP
atomic instruction used in multi-threading to achieve synchronization
ํน์ ๊ฐ์ด (false), ์์ํ ๊ฐ (false) ๊ณผ ๊ฐ๋ค๋ฉด ์๋ก์ด ๊ฐ์ผ๋ก ์นํํ๋ ๋ฐฉ๋ฒ.
๋ง์ฝ์ ํน์ ๊ฐ์ด (true), ์์ํ ๊ฐ(false) ๊ณผ ๋ค๋ฅด๋ฉด ํด๋น ์นํ์์ ์ ์คํจํจ์ด ๋ณด์ฅ๋จ.
code
java8
java.util.concurrent.atomic.AtomicInteger#incrementAndGet
var5
๋ volatile value๋ฐํํ๋ ๊ฑด ์ด๊ธฐ๊ฐ (e.g = 1)
val1
๋ AtomicInteger ํด๋์คvar2
๋ valueOffSetThe offset is just used to determine which memory location to address when updating the value field of AtomicInteger instances.
AtomicInteger ์ธ์คํด์ค๊ฐ ๊ฐ์ง๊ณ ์๋ value๊ฐ์ ์ฃผ์์ธ๋ฏ.
compareAndSwapInt
CAS ๊ธฐ๋ฒ์ผ๋ก, do-while๋ฌธ์ ๋๋ฉด์ ํ์ฌ ๋ฉ์ธ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ด var5์ ๊ฐ์ผ๋ฉด var5 + var4(1) ํด์ setํ๊ณ ์๋๋ฉด ๊ณ์ ๋๋ฏ.๋๋ค๊ฐ ์ด๋์ ๋ ์ง๋๋ฉด ํ๊ธฐ๊ฒ ์ง..?
์ฐธ๊ณ
https://www.digitalocean.com/community/tutorials/atomicinteger-java
https://www.javacodemonk.com/what-is-atomicinteger-class-and-how-it-works-internally-1cda6a56
https://www.baeldung.com/java-volatile
https://www.geeksforgeeks.org/volatile-keyword-in-java/#:~:text=For%20Java%2C%20%E2%80%9Cvolatile%E2%80%9D%20tells,scope%20of%20the%20program%20itself.
Last updated