Item 78. Mutable Data Sharing
Java์์ ๊ฐ๋ณ ๋ฐ์ดํฐ ์ค๋ ๋๊ฐ ํต์
class StopThread {
private static boolean stopRequested;
public static void main(String[] args) throws InterruptedException {
Thread backgroundThread = new Thread(() -> {
int i = 0;
while (!stopRequested) {
i++;
System.out.println(i);
}
});
backgroundThread.start();
TimeUnit.SECONDS.sleep(1);
stopRequested = true;
}
}synchronized ๋ฉ์๋๋ฅผ ์ด์ฉํ ๋๊ธฐํ
volatile ํ๋๋ฅผ ์ด์ฉํ ๋๊ธฐํ
volatile ์ฃผ์์ฌํญ
synchronized ๋ธ๋ก์ ์ด์ฉํ ๋๊ธฐํ
Atomic ํด๋์ค๋ฅผ ์ด์ฉํ ๋๊ธฐํ
๊ฒฐ๋ก
Last updated