Monday, June 11, 2012
Why are wait(), notify(), and notifyAll() in the Object class in java?
After all, these methods have something to do with threads, so why aren't they in the thread class?The answer is that Threads can use Objects to transmit messages from one thread to another, and these methods allow that to happen. A Thread calls wait() to say "I am waiting for a message to be sent to this object." Another thread can call notify() to say "Iam sending a message to that object." The Object is therefore a conduit through which threads communicate without explicitly referencing each other. If the methods were in theThread class, then two threads would need to have references to one another to communicate. Instead, all communicating threads just need to agree to use some specific shared resource