• 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

Question about wait() method

 
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from ExamsLab-5.0 final exam.
The doubt I have about this question is why wait method is not inside any synchronize block like this.

Actually both wait() and notify() method should be called from a synchronized block and by this Is owns a lock on same object, So I suspected IllegalMonitorException, but this questions. resulted in "Ready to Print, Now Printing, ". I ran it, It worked fine. I am still confused about it



Regards
Balraj
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should these methods be called from a synchronized context.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is important.....to acquire a lock on the object before the wait(this.wait here) on this object or having a synchronized block? I guess this distinction will answer your question?
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friend.....

the correct rule is wait and notify should be called while a thread is inside a monitor....
here when the thread executes synchronized method it is obviously inside the monitor of the implicit "this" object.....


so these methods(wait,notify,notifyAll) can be called
either from 1)synchronized block
or from 2)synchronized method
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone humor me and post the code it would take to actually print the last "Printed".
Thanks in advance.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balraj Momi wrote:Actually both wait() and notify() method should be called from a synchronized block and by this Is owns a lock on same object



I don't know what you mean by this but if you see, the call to wait in doMore is indeed in a synchronized context. The method doMore is synchronized. So it will have a lock on the this reference and the call to wait is just like calling this.wait(). you don't need to have a lock on the thread object. You are calling wait on the object on which the doMore method was called.

Sunny Narula wrote:Can anyone humor me and post the code it would take to actually print the last "Printed".



For that you need to notify on the object which is passed to the Thread constructor. Like this



But this code will only work if doMore is called before the main method enters the synchronized block. The sleeps will handle it nicely...
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As ankit said for printed to print you have to see on what object its waiting. Here it is this.wait() means trd class. So we have to notify on the object only. The original code won't be useful as there is an anonymous instance created but ankit's scenario is good for the "printed"
 
Sunny X Narula
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit
 
reply
    Bookmark Topic Watch Topic
  • New Topic