Hi, wait(), notify(), and notifyAll() methods cannot be called outside a synchronized method or synchronized block. All of them throw IllegalMonitorStateException. Conclusion - These method calls make no sense outside the context of multithreading. Given these facts, doesn't it make better sense to have them all inside Thread class? What is the rationale in keeping them in Object class?
If these methods were inside the Thread class, you would have to have a reference to all the threads that needed to wait/notify on your object and call the method for each one. Why would that make sense to you?
Ramesh, I'm guessing that you're under the impression that the object on which the synchronization is based is always going to be a Thread object, which is why it may seem to you that those methods should have been defined in that class. For example, you've probably seen code like the following:
In this case, the Thread class uses its own monitor for synchronization, which is pretty common practice because of its convenience and simplicitly. However, when you get into a more sophisticated multi-thread application, it's often helpful or necessary to synchronize on a non-Thread object. For example, let's suppose that you've created an instance of ArrayList and you want to write code that will cause one thread to remove an entry from the list or wait until some other thread adds an entry to the list. In that scenario, you could write code like the following:
You'd probably also somewhere have code like the following that would add an entry to the list:
Note that the synchronization (and therefore the wait() and notifyAll() calls) are done using the ArrayList object rather than a Thread instance. If those methods were defined in Thread instead of Object, creating functionality like that shown above would be more difficult. ------------------ Brett Spell Author, Professional Java Programming