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.

Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
AmitN
  • 73
  • 3
  • 12
  • The most basic reason is that for wait(), notify(), and notifyAll() to be useful multiple threads have to execute them using the same object. – Patricia Shanahan Mar 06 '13 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 `Thread`s. – Dave Newton Mar 06 '13 at 14:18

3 Answers3

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...

Community
  • 1
  • 1
bsiamionau
  • 8,099
  • 4
  • 46
  • 73
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).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

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

PSR
  • 39,804
  • 41
  • 111
  • 151