2 Replies Latest reply on Feb 21, 2007 1:59 PM by 807606

    wait and notify


      Why wait and notify methods belong to Object class when we use them only with threads. Shouldnt it be within Thread class ?

      This was one interview questions asked to me.
        • 1. Re: wait and notify
          Never thought about it before, but I'm guessing that each object has to be run by a thread, so even if you don't implement runnable or extend thread, there is still a thread running the Java application.
          • 2. Re: wait and notify
            The wait() and notify() methods are object-specific. The wait() method suspends the current thread of execution, and tells the object to keep track of the suspended thread. The notify() method tells the object to wake up the suspended threads that it is currently keeping track of. Since wait() and notify() are object specific, they must be used within code that is synchronized on the object in question.