1. Java Concurrency: Visibility and Synchronized
https://dzone.com/articles/java-concurrency-visibility-and-synchronized
[ ์์ฝ ]
๋ ์ค๋ ๋ ๊ฐ์ ๋ณ์ ๊ฐ์์ฑ๊ณผ, ๊ณต์ ๋ณ์๋ฅผ ๋ณ๊ฒฝํ ๋ ๋ฐ์ํ๋ ์ผ์ ๋ํด ์์๋ณด์
์ง์ํด์ผ ํ ์ฝ๋
import java.util.Date;
public class UnSynchronizedCountDown {
private int number = Integer.MAX_VALUE;
public Thread countDownUntilAsync(final int threshold) {
return new Thread(() -> {
while (number>threshold) {
number--;
System.out.println("Decreased "+number +" at "+ new Date());
}
});
}
private void waitUntilThresholdReached(int threshold) {
while (number>threshold) {
}
}
public static void main(String[] args) {
int threshold = 2125840327;
UnSynchronizedCountDown unSynchronizedCountDown = new UnSynchronizedCountDown();
unSynchronizedCountDown.countDownUntilAsync(threshold).start();
unSynchronizedCountDown.waitUntilThresholdReached(threshold);
System.out.println("Threshold reached at "+new Date());
}
}
์ฃผ์ด์ง ์ฝ๋๋ ๋ฉํฐ์ค๋ ๋ ํ๋ก๊ทธ๋๋ฐ์์ ๋ฉ๋ชจ๋ฆฌ ๊ฐ์์ฑ๊ณผ ๋๊ธฐํ์ ๊ด๋ จ๋ ๋ฌธ์ ๊ฐ ์๋ ์ฝ๋๋ค.
์ฃผ์ด์ง ์ฝ๋๋ ๋ ๊ฐ์ ์ค๋ ๋๊ฐ 'number'๋ผ๋ ๋์ผํ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ ํ๋ค.
countDownUntilAsync()๋ 'number' ๋ฅผ ๊ฐ์์ํค๋ ์์ ์ ์ํํ๋ค.
main()๋ 'number' ๊ฐ ํน์ ์๊ณ๊ฐ ์ดํ๋ก ๋จ์ด์ง ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
ํฌ๊ฒ ๋ ๊ฐ์ง ๋ฌธ์ ์ ์ด ์กด์ฌํ๋ค.
๋ฌธ์ ์ 1: ๋๊ธฐํ ๋ถ์กฑ
Java์์ ์ฌ๋ฌ ์ค๋ ๋๊ฐ ๊ฐ์ ๋ณ์๋ฅผ ๋์์ ์ ๊ทผํ๊ณ ๋ณ๊ฒฝํ ๋, ์ด ๋ณ์๋ฅผ ๋๊ธฐํํ์ง ์์ผ๋ฉด ์๊ธฐ์น ์์ ๊ฒฐ๊ณผ๊ฐ ๋ฐ์ํ ์ ์๋ค.
- ๋ณ์ number๊ฐ ๋ ๊ฐ์ ์ค๋ ๋์์ ๋์ ์ ๊ทผ์ ํ๋ค
- ์ฒซ ๋ฒ์งธ ์ค๋ ๋๋ ๋ณ์ซ๊ฐ ๊ฐ์, ๋ ๋ฒ์งธ ์ค๋ ๋๋ ๋ณ์๋ฅผ ์ฝ๊ณ ์๋ค
- ์ฌ๊ธฐ์, ๋ ์ค๋ ๋๊ฐ ๋์์ number์ ์ ๊ทผํ ๋ ์ด๋ฅผ ์ ์ ํ ๋๊ธฐํํ์ง ์์, ๋ ์ค๋ ๋๊ฐ ์๋ก์ ์์ ๊ฒฐ๊ณผ๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ธ์ํ์ง ๋ชปํ ๊ฐ๋ฅ์ฑ์ด ์๋ค
๋ฌธ์ ์ 2: ๋ฉ๋ชจ๋ฆฌ ๊ฐ์์ฑ ๋ฌธ์
๊ฐ ์ค๋ ๋๊ฐ ์์ ๋ง์ ์บ์๋ฅผ ๊ฐ์ง๊ณ ์์ ์ ์ํํ๊ฒ ๋๋ค๋ฉด, ํ ์ค๋ ๋๊ฐ ํน์ ๋ณ์์ ๊ฐ์ ๋ณ๊ฒฝํ๋๋ผ๋ ๋ค๋ฅธ ์ค๋ ๋์์ ๊ทธ ๋ณ๊ฒฝ ์ฌํญ์ ๋ฐ๋ก ์ธ์ํ์ง ๋ชปํ ์ ์๋ค.
- ์ฒซ ๋ฒ์งธ ์ค๋ ๋๋ number๋ฅผ ๊ฐ์์ํค์ง๋ง, ๋ฉ์ธ์์๋ ๋ณ๊ฒฝ๋ ๊ฐ์ ์ฝ์ ์ ์๋ค.
- ๋ฐ๋ผ์, ๋ฉ์ธ ์ค๋ ๋๋ number ๊ฐ์ด ์๊ณ๊ฐ ์ดํ๋ก ๋จ์ด์ ธ๋ ์ด๋ฅผ ์ธ์ํ์ง ๋ชปํด ๋ฌดํ ๋ฃจํ์ ๋น ์ง ์ ์
ํด๊ฒฐโก๏ธ ๋๊ธฐํ(synchronized) ์ฌ์ฉ
- synchronized ๋ธ๋ก์ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ์ค๋ ๋๊ฐ ๋์์ ๋ณ์ number์ ์ ๊ทผํ๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์๋ค
- synchroinzed ๋ธ๋ก์ ๊ฑธ๋ ค ๋์ด์ง ๋ค๋ฅธ ์ค๋ ๋์์ ์ค๋ ๋์ ๋ณ๊ฒฝ ์ฌํญ์ ๋ณผ ์ ์๋ค
package com.gkatzioura.concurrency.visibility;
public class SynchronizedCountDown {
private int number = Integer.MAX_VALUE;
private String message = "Nothing changed";
private static final Object lock = new Object();
private int getNumber() {
synchronized (lock) {
return number;
}
}
public Thread countDownUntilAsync(final int threshold) {
return new Thread(() -> {
message = "Count down until "+threshold;
while (number>threshold) {
synchronized (lock) {
number--;
if(number<=threshold) {
}
}
}
});
}
private void waitUntilThresholdReached(int threshold) {
while (getNumber()>threshold) {
}
}
public static void main(String[] args) {
int threshold = 2147270516;
SynchronizedCountDown synchronizedCountDown = new SynchronizedCountDown();
synchronizedCountDown.countDownUntilAsync(threshold).start();
System.out.println(synchronizedCountDown.message);
synchronizedCountDown.waitUntilThresholdReached(threshold);
System.out.println(synchronizedCountDown.message);
}
}
getNumber()์์ ๋๊ธฐํ ๋ธ๋ก์ ํตํด number ๊ฐ์ ๋ฐํํ๋ค.
์ด๋ก ์ธํด ํด๋น ๋ฉ์๋์ ์ ๊ทผํ๋ ์ค๋ ๋๋ lock ๊ฐ์ฒด๋ฅผ ์ ๊ถ, ๋ค๋ฅธ ์ค๋ ๋๊ฐ ํด๋น ๋ธ๋ก์ ๋ค์ด์ค๋ ๊ฒ์ ๋ง๋๋ค.
countDownUntilAsync()์์ number ๊ฐ์์ํค๋ ๋ก์ง์ ๋ธ๋ก ์์ ๋ฃ์ด, lock ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ฌ ์ ๊ทผ๋ค.
number ๋ณ์ ๊ฐ ๋ณ๊ฒฝ์ด ๋ค๋ฅธ ์ค๋ ๋์๋ ์ ๋ฐ์๋๋๋ก ํ๋ค.
์ ์์ ๋ ์ฝ๋๋ฅผ ํตํด ๊ฐ์์ฑ์ด ๋ณด์ฅ๋๋ค.
โ
Java ๊ฐ์์ฑ ๋ณด์ฅ?
์ฌ๋ฌ ์ค๋ ๋๊ฐ ๋์ผํ ๋ณ์์ ์ ๊ทผํ ๋, ํ ์ค๋ ๋๊ฐ ํด๋น ๋ณ์๋ฅผ ๋ณ๊ฒฝํ ๊ฒ์ด ๋ค๋ฅธ ์ค๋ ๋์์๋ ์ฆ์ ํ์ธ๋ ์ ์๋๋ก ํ๋ ๊ฒ
โก๏ธ ์ค๋ ๋ ๊ฐ์ ์ผ๊ด๋ ๋ฐ์ดํฐ ๊ณต์ ๋ฅผ ๋ณด์ฅ
- synchronized ๋ธ๋ก์ด ์ฌ์ฉ๋๋ฉด, ํด๋น ๋ธ๋ก ๋ด์์ ๋ณ๊ฒฝ๋ ๋ชจ๋ ๋ณ์์ ์ํ๋ ๋ธ๋ก์ด ๋๋ ๋ ๋ฉ์ธ ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์๋จ
- ์ด๋ก ์ธํด, ๋ค๋ฅธ ์ค๋ ๋๊ฐ ๋์ผํ lock์ ์ฌ์ฉํ์ฌ ๋๊ธฐํ๋ ๋ธ๋ก์ ์ ๊ทผํ ๋ ๋ณ์์ ์ต์ ๊ฐ์ ๋ณผ ์ ์๊ฒ ๋๋ค
๐ค ์ด์ ๋ํ ๋์ ์๊ฐ
๋๋ฒ๊น ์ผ๋ก ๋ณด์ด์ง ์๋ ์ค๋ฅ๋ผ, ๊ฐ์์ฑ ๋ฌธ์ ์ ์ค์์ฑ์ด ๋๋ค๊ณ ์๊ฐํ๋ค.
๋ ๋ํ ๊ฐ์์ฑ์ ์ค์์ฑ์ ํฌ๊ฒ ์๊ฐํ์ง ์์์๋๋ฐ, ์์์น ๋ชปํ ๋์์ด ๋ฐ์ํ๊ณ ํนํ ๋๋ฒ๊น ์ด ์ด๋ ต๋ค๋ ์ ์์ ์ค์ํจ์ ๊นจ๋ฌ์๋ค.
synchronized๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์์ฑ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์์ง๋ง, ์ฑ๋ฅ ๋น์ฉ์ ๋ฌธ์ ๊ฐ ์๋ค. ์ค๋ ๋์ ์์ ๋๊ธฐ ์๊ฐ์ ๊ณง ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฑ๋ฅ๊ณผ๋ ์ฐ๊ฒฐ๋๋ค.
๋ฐ๋ผ์ ๋ฌด์์ ๋๊ธฐํ๋ฅผ ์ฑ๊ธฐ๊ธฐ๋ณด๋ค "์ฑ๋ฅ์ด ์ค์ํ ์ ํ๋ฆฌ์ผ์ด์ ์์๋ ์ ์ ํ ์ฌ์ฉํ๋ ๊ฒ" ์ด ์ค์ํด ๋ณด์ธ๋ค.
'๐๏ธ IT ๋ํฅ ํ์ ๋ฐ ๋์ ์๊ฐ ์ ๋ฆฌ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Linux: SSH ๋ฐ Key-Based ์ธ์ฆ (0) | 2024.09.02 |
---|---|
๋ฆฌ๋ ์ค xz ๊ฒฐํจ: ๊ธฐ์ ์ ๋ฌธ์ ๊ฐ ์๋ ์ค๋ฆฌ์ ๋ฌธ์ (0) | 2024.08.30 |
[Passkeys & ์ ๋ก ํธ๋ฌ์คํธ] ์ฌ์ฉ ํ๋ํ: "๋น๋ฐ๋ฒํธ๋ฅผ ์์ ์" (1) | 2024.08.28 |
[CORS] GET vs Optional Methods (POST, PUT, OPTIONS) (0) | 2024.08.27 |
ํฅ์๋ Cyber Resilience๋ฅผ ์ํ Flask + RDF: ์๋งจํฑ ์น ๊ธฐ์ ํ์ฉ (0) | 2024.08.26 |