0

We have start(), run() and join() methods with threads as we use them on thread objects, these methods are under Thread class.

And wait(), notify(), and notifyAll() which we use also with Thread object but these methods are under Object class.

I Am thinking why there is need to define these methods under Object class as we call them only with Threads?. We know whenever we call wait() object goes to the wait state waiting for a Shared resource/a condition to satisfy but this can only happen in threaded environment.

So the question is why there is need to put these methods under Object class?

Let me know if I am missing anything here.

2
  • The most basic reason is that for wait(), notify(), and notifyAll() to be useful multiple threads have to execute them using the same object. Commented Mar 6, 2013 at 14:17
  • Don't thread on me. You're missing a bunch of "h". Objects can be the subject of waits/notifications even if they're not Threads. Commented Mar 6, 2013 at 14:18

3 Answers 3

2

Methods wait()/notify()/notifyAll() belong to object because they work with this object as with monitor.

Similar questions:

Try to seach for same questions on SO before asking...

Sign up to request clarification or add additional context in comments.

1 Comment

@zvzdhk.. Thanks, sure will take care of it next time.
0

Each Java object has a monitor associated with it. The functions that you refer to operate on the object's monitor. This is the reason they need to be defined under Object (they could have been made static members of some other class, but then they would need to take an argument of type Object anyway).

Comments

0

Object class is the super class for all java classes.The methods in Object we can use in any class for synchronization.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.