개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.07.11 08:25

Threaded::notify 관련 질문

목록
  • 루트권한 6일 전 2025.07.11 08:25
  • 40
    1
제가 Java에서 멀티 쓰레딩을 공부중인데, Threaded::notify() 메서드에 대한 이해가 부족합니다.

바로 질문을 드리겠습니다.

notify() 메서드는 어떤 상황에서 호출되어야 하는지,

또한 wait() 메서드와 notify() 메서드가 동시에 호출될 경우

어떻게 동작하는지,

또한 notifyAll() 메서드와 notify() 메서드의 차이점에 대해 설명해주실 수 있을까요?

    댓글목록

    profile_image
    나우호스팅  6일 전



    Java의 Thread 클래스에서 notify() 메서드는 한 쓰레드가 다른 쓰레드에 알림을 보내는 용도로 사용됩니다. notify() 메서드는 wait() 메서드가 호출된 쓰레드에게만 알림을 보냅니다.

    wait() 메서드와 notify() 메서드가 동시에 호출될 경우, notify() 메서드는 wait() 메서드가 호출된 쓰레드에게만 알림을 보냅니다. 만약 notify() 메서드가 호출된 쓰레드가 wait() 메서드를 호출하지 않은 경우, notify() 메서드는 아무런 효과가 없습니다.

    notifyAll() 메서드는 모든 쓰레드에 알림을 보냅니다. notify() 메서드와의 차이점은 notify() 메서드는 한 쓰레드에게만 알림을 보낸다면, notifyAll() 메서드는 모든 쓰레드에게 알림을 보냅니다.

    예를 들어, 다음과 같은 코드를 살펴보겠습니다.

    #hostingforum.kr
    java
    
    public class NotifyExample {
    
        public static void main(String[] args) {
    
            final Object lock = new Object();
    
            Thread thread1 = new Thread(() -> {
    
                synchronized (lock) {
    
                    System.out.println("쓰레드 1: wait() 메서드 호출");
    
                    try {
    
                        lock.wait();
    
                    } catch (InterruptedException e) {
    
                        Thread.currentThread().interrupt();
    
                    }
    
                    System.out.println("쓰레드 1: notify() 메서드 호출");
    
                }
    
            });
    
            
    
            Thread thread2 = new Thread(() -> {
    
                synchronized (lock) {
    
                    System.out.println("쓰레드 2: notify() 메서드 호출");
    
                    lock.notify();
    
                }
    
            });
    
            
    
            thread1.start();
    
            thread2.start();
    
        }
    
    }
    
    


    위 코드에서 thread1 쓰레드는 wait() 메서드를 호출하고, thread2 쓰레드는 notify() 메서드를 호출합니다. thread1 쓰레드는 notify() 메서드가 호출된 쓰레드에게만 알림을 받을 수 있습니다.

    notifyAll() 메서드는 다음과 같이 사용할 수 있습니다.

    #hostingforum.kr
    java
    
    public class NotifyAllExample {
    
        public static void main(String[] args) {
    
            final Object lock = new Object();
    
            Thread thread1 = new Thread(() -> {
    
                synchronized (lock) {
    
                    System.out.println("쓰레드 1: wait() 메서드 호출");
    
                    try {
    
                        lock.wait();
    
                    } catch (InterruptedException e) {
    
                        Thread.currentThread().interrupt();
    
                    }
    
                    System.out.println("쓰레드 1: notifyAll() 메서드 호출");
    
                }
    
            });
    
            
    
            Thread thread2 = new Thread(() -> {
    
                synchronized (lock) {
    
                    System.out.println("쓰레드 2: notifyAll() 메서드 호출");
    
                    lock.notifyAll();
    
                }
    
            });
    
            
    
            thread1.start();
    
            thread2.start();
    
        }
    
    }
    
    


    위 코드에서 thread1 쓰레드는 notifyAll() 메서드를 호출하여 모든 쓰레드에게 알림을 보냅니다.

    2025-07-11 08:26

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 36,596건 / 2 페이지

검색

게시물 검색