• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: https://coderanch.com/t/233340/threads/java/Why-java-lang-Object

Thanks,
Raghavan alias Saravanan M.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic