• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

why wait()/notify() methods are in Object class?

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
wait(), notify(), and notifyAll() methods cannot be called outside a synchronized method or synchronized block. All of them throw IllegalMonitorStateException. Conclusion - These method calls make no sense outside the context of multithreading.
Given these facts, doesn't it make better sense to have them all inside Thread class? What is the rationale in keeping them in Object class?
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If these methods were inside the Thread class, you would have to have a reference to all the threads that needed to wait/notify on your object and call the method for each one. Why would that make sense to you?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
would you want to extend Thread on every class that may be accessed by multiple threads? This way any class (object) can protect itself from multiple thread access
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh,
I'm guessing that you're under the impression that the object on which the synchronization is based is always going to be a Thread object, which is why it may seem to you that those methods should have been defined in that class. For example, you've probably seen code like the following:

In this case, the Thread class uses its own monitor for synchronization, which is pretty common practice because of its convenience and simplicitly. However, when you get into a more sophisticated multi-thread application, it's often helpful or necessary to synchronize on a non-Thread object. For example, let's suppose that you've created an instance of ArrayList and you want to write code that will cause one thread to remove an entry from the list or wait until some other thread adds an entry to the list. In that scenario, you could write code like the following:

You'd probably also somewhere have code like the following that would add an entry to the list:

Note that the synchronization (and therefore the wait() and notifyAll() calls) are done using the ArrayList object rather than a Thread instance. If those methods were defined in Thread instead of Object, creating functionality like that shown above would be more difficult.
------------------
Brett Spell
Author, Professional Java Programming
 
Ramesh Donnipadu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bret!
Not only did you give excellent answer, but you guessed why I had this impression in the first place. Thanks again.
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic