| Author |
Question about wait() method
|
Balraj Momi
Ranch Hand
Joined: Jul 23, 2009
Posts: 45
|
|
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
|
Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
|
Why should these methods be called from a synchronized context.
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
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?
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
karthick chinnathambi
Ranch Hand
Joined: Jul 06, 2009
Posts: 196
|
|
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
|
KARTHICK.C , SCJP6-93%
(Born to Win)
|
 |
Sunny X Narula
Greenhorn
Joined: Dec 07, 2008
Posts: 22
|
|
Can anyone humor me and post the code it would take to actually print the last "Printed".
Thanks in advance.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
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
Joined: Dec 07, 2008
Posts: 22
|
|
|
Thanks Ankit
|
 |
 |
|
|
subject: Question about wait() method
|
|
|