Java

[Effective Java] ์•„์ดํ…œ 78. ๊ณต์œ  ์ค‘์ธ ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ๋Š” ๋™๊ธฐํ™”ํ•ด ์‚ฌ์šฉํ•˜๋ผ.

quedevel 2023. 4. 12. 10:10
728x90
๋ฐ˜์‘ํ˜•

๐ŸŽฏ ์•„์ดํ…œ 78. ๊ณต์œ  ์ค‘์ธ ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ๋Š” ๋™๊ธฐํ™”ํ•ด ์‚ฌ์šฉํ•˜๋ผ.

synchronized ํ‚ค์›Œ๋“œ๋Š” ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋‚˜ ๋ธ”๋ก์„ ํ•œ๋ฒˆ์— ํ•œ ์Šค๋ ˆ๋“œ์”ฉ ์ˆ˜ํ–‰ํ•˜๋„๋ก ๋ณด์žฅํ•œ๋‹ค. ํ•œ ๊ฐœ์ฒด๊ฐ€ ์ผ๊ด€๋œ ์ƒํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์ƒ์„ฑ๋˜๊ณ , ์ด ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๊ทธ ๊ฐ์ฒด์— ๋ฝ(lock)์„ ๊ฑด๋‹ค. ๋ฝ์„ ๊ฑด ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ํ™•์ธํ•˜๊ณ  ํ•„์š”ํ•˜๋ฉด ์ˆ˜์ •ํ•œ๋‹ค. ์ฆ‰, ๊ฐ์ฒด๋ฅผ ํ•˜๋‚˜์˜ ์ผ๊ด€๋œ ์ƒํƒœ์—์„œ ๋‹ค๋ฅธ ์ผ๊ด€๋œ ์ƒํƒœ๋กœ ๋ณ€ํ™”์‹œํ‚จ๋‹ค.


๋™๊ธฐํ™” ์—†์ด๋Š” ํ•œ ์Šค๋ ˆ๋“œ๊ฐ€ ๋งŒ๋“  ๋ณ€ํ™”๋ฅผ ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์—์„œ ํ™•์ธํ•˜์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ๋‹ค. ๋™๊ธฐํ™”๋Š” ์ผ๊ด€์„ฑ์ด ๊นจ์ง„ ์ƒํƒœ๋ฅผ ๋ณผ ์ˆ˜ ์—†๊ฒŒ ํ•˜๋Š” ๊ฒƒ์€ ๋ฌผ๋ก , ๋™๊ธฐํ™”๋œ ๋ฉ”์„œ๋“œ๋‚˜ ๋ธ”๋ก์— ๋“ค์–ด๊ฐ„ ์Šค๋ ˆ๋“œ๊ฐ€ ๊ฐ™์€ ๋ฝ์˜ ๋ณดํ˜ธํ•˜์— ์ˆ˜ํ–‰๋œ ๋ชจ๋“  ์ด์ „ ์ˆ˜์ •์˜ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ฒŒ ํ•ด์ค€๋‹ค.

๋™๊ธฐํ™”๋Š” ๋ฐฐํƒ€์  ์‹คํ–‰๋ฟ ์•„๋‹ˆ๋ผ ์Šค๋ ˆ๋“œ ์‚ฌ์ด์˜ ์•ˆ์ •์ ์ธ ํ†ต์‹ ์— ๊ผญ ํ•„์š”ํ•˜๋‹ค.

  • ์ž˜๋ชป๋œ ์ฝ”๋“œ - ์ด ํ”„๋กœ๊ทธ๋žจ์€ ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜ ์‹คํ–‰๋ ๊นŒ?
public class StopThread {

    private static boolean stopRequested;

    public static void main(String[] args) throws InterruptedException {
        Thread backgroundThread = new Thread(() -> {
            int i = 0;
            while (!stopRequested){
                i++;
            }
        });
        backgroundThread.start();

        TimeUnit.SECONDS.sleep(1);
        stopRequested = true;

    }
}

  • ์ ์ ˆํžˆ ๋™๊ธฐํ™”ํ•ด ์Šค๋ ˆ๋“œ๊ฐ€ ์ •์ƒ ์ข…๋ฃŒํ•œ๋‹ค.
public class StopThread {

    private static boolean stopRequested;

    private static synchronized void requestStop(){
        stopRequested = true;
    }

    private static synchronized boolean stopRequested(){
        return stopRequested;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread backgroundThread = new Thread(() -> {
            int i = 0;
            while (!stopRequested()){
                i++;
            }
        });
        backgroundThread.start();

        TimeUnit.SECONDS.sleep(1);
        requestStop();
    }
}

  • volatile ํ•„๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์Šค๋ ˆ๋“œ๊ฐ€ ์ •์ƒ ์ข…๋ฃŒํ•œ๋‹ค. volatile ํ‚ค์›Œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉ”์ธ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅํ•˜๊ณ  ์ฝ์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ ๊ฐ’ ๋ถˆ์ผ์น˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
public class StopThread {

    private static volatile 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;
    }
}

์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•œ๋‹ค๋ฉด ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š” ๋™์ž‘์€ ๋ฐ˜๋“œ์‹œ ๋™๊ธฐํ™” ํ•ด์•ผํ•œ๋‹ค.


์ฐธ๊ณ  ์ž๋ฃŒ

Joshua Bloch, ใ€ŽEffective Java 3/Eใ€, ๊ฐœ์•ž๋งต์‹œ ์˜ฎ๊น€, ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ธ์‚ฌ์ดํŠธ(2018)
http://www.kyobobook.co.kr/product/detailViewKor.laf?ejkGb=KOR&mallGb=KOR&barcode=9788966262281&orderClick=LEa&Kc=

728x90
๋ฐ˜์‘ํ˜•