Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about wait() method

 
Balraj Momi
Ranch Hand
Posts: 45
Java
  • Mark post as helpful
  • send pies
  • 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
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should these methods be called from a synchronized context.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • 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?
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • 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
 
Sunny X Narula
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 Java Python
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Ankit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic