Java

[Java] synchronized에 대해서 모르겠는 점

Dong's Universe 2023. 9. 8. 15:21
public class ThreadA {
    public static void main(String[] args) {
        ThreadB b = new ThreadB();
        b.start();

        synchronized (b) {
            try {
                System.out.println("B가 끝날 때까지 기다립시다.");
                b.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(b.total);
        }
    }
}

왜 synchronized (b)를 넣어주는 것일까?

Reference


https://school.programmers.co.kr/learn/courses/9/lessons/278

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr