• Post Reply Bookmark Topic Watch Topic
  • New Topic

wait(), notify() methods of Object class?

 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've a small doubt, i.e. wait(), notify(), notifyAll() are the methods we use oftenly to control threads, why they are available in Object class instead of Thread class?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the things you are waiting for are objects, not necessarily threads.
You wait for an internet connection to be there, or a list to be sorted for example.

You can also wait for a thread to be ready, but this is done with the join() method (for example) and join is in class threads.

OK?

Yours,
Bu
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Suhas Madap:
Hi,
I've a small doubt, i.e. wait(), notify(), notifyAll() are the methods we use oftenly to control threads, why they are available in Object class instead of Thread class?


I would say these methods are lock related , not Thread related.So these methods have been provided in all the classes by including them in the Object class as all objects have a lock.
 
Suhas Madap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much,
What I understood with comments that wait(), notify() methods are used to lock the objects. Am I correct? but it would be so kind,can explain further.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you make a Thread wait for sometime?

One way is to call wait on some lock.

How to specify which lock to use for this ?

You have to call wait in a synchronized context , so for attaining synchronization you need a lock and that lock would be used for wait.After the execution of wait the thread would be blocked in the wait set for that lock and will be in that state untill it times out or some other thread notifies it.You should be able to call wait from any class , not only from Thread class .So it has been put in Object class not in Thread class.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have asked the same question sometimes back. I would like you to go through the answers from the experts for the good justification.

Hope it helps. Please visit it here: http://www.coderanch.com/t/233340/threads/java/Why-java-lang-Object

Thanks,
Raghavan alias Saravanan M.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!