Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why wait and notify are methods of Object?

 
vikasids sharma
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why wait(), notify() and notifyAll() are methods of Object class and not Thread class when these methods are called for threads only?
Thanks,
Vikas
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods are not only called on Threads.
The purpose of these methods is to provide a way to prevent an Object from being accessed from two different Threads at the same time. These methods allow Threads to have exclusive access to an object for the duration of an operation that should be considered atomic.
The reason they are in the Object class is because we must be able to make sure that any Object can be protected from concurrent access.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic