File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait()" Watch "wait()" New topic
Author

wait()

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Dr.Hunt's mock exam Q # 39

What is the effect of issuing a wait() method on an object
A. If a notify() method has already been sent to that object then it has no effect
B. The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
C. An exception will be raised
D. The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.

the given ans is (B)
i am not clear on the part
"until another object sends a notify() or notifyAll() method " doesnt the current object sends notify()?
Thanks.
------------------

[This message has been edited by Shiva (edited April 15, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Shiva,
one small request. Can you tell us the mock exam name and the qstn no? To maintain the quality of our forum, it is requested from everyone, if you post any qstn please tell us from where did you get it, which mock exam and if possible the qstn no. also.
And one more point. Please do not just post the qstn like this. Try to tell us what's your answers and the reasoning behind them. This will help us to easily continue the discussion.
regds
maha anna
[This message has been edited by maha anna (edited April 15, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Maha,
i wasnt finding that exam so couldnt mention the Q No,
but you are right!
i have edited the post.
thanks!
------------------
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Thanks Shiva.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Shiva,
When a thread calls wait(..) , it releases the lock which it has currently,( because in order to able to call wait() it thread must get the object's lock first ) and goes to the waiting state. This thread is waiting for some other thread to notify it. Which means this thread which called wait() is waiting for the lock of the object which it had released. Every object has a pool of waiting thread who are all waiting (competing) to grab the object's lock. Since a object has only one lock, this lock can be with only one thread at a time. the The thread which had issued wait() is calmly waiting right now, and when it is notified by another thread which owns the same lock by means of notify()/notifyAll(), then it competes with all other threads who are all in the same boat like this, and if it manages to grab the lock then , it goes to ready state. Now, it is waiting for green signal from the scheduler.
regds.
So the given ans is correct.
maha anna
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
Why Cis not correct??
wait() throws InterruptedException.Am I right??
Carol Willing
Greenhorn

Joined: Apr 10, 2000
Posts: 2
Shiva,
Sun's tutorial has an application and explanation of using wait/notifyAll. It is in the Java Essentials trail in the Threads lesson. I don't have the exact link or I would post it for you. Good luck.
Carol
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Mani,
Ans c) An exception will be raised
The wait() has to be inside a try-catch-InterrupredException. This is true. But it does not mean an Exception will be raised when you issue a wait(). When will an InterruptedException occur? When another thread issues a notify() or notifyAll() or an interrupt() on this thread right?. So there is a chance that there may be an InterruptedException to occur. What if none of the other thread care at all ? This poor thread which issued wait() and waiting for the lock of the object which it had released before, keeps on waiting for someone to notify this. So only it is good idea to put sleep(..) or yield() or wait(millisec) or wait(millsec,nanosec). But still there is a difference between all these sleep(..) yield () wait(sometime). The difference is yield() yields only to other threads of the SAME PRIORITY and DOES NOT RELEASE the lock. sleep() also DOES NOT RELEASE THE LOCK but it allows other low-priority ones also to get a chance. But wait() /wait(..) RELEASES ITS LOCK naively and depends on others to get its lock back in order to go to ready state
regds
maha anna
[This message has been edited by maha anna (edited April 17, 2000).]
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
Nice explanation Maha
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
I agree with Mani. The depth of your knowledge, maha anna, just amazes me sometimes. Where do you get your info from, or do just really think things thru. Your explanations (and Jim's) are a pleasure to read!
Betty Reynolds
Ranch Hand

Joined: Feb 16, 2000
Posts: 111
Maha,
The info on this is not very good. I just read somewhere that wait() and yield() releases the lock (I'm going to stop mentioning the source of misinformation).
This never really made sense to me. Why bother to enqueue on an object, if you are going to release the lock. Are you sure that wait does release the lock?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Yes. Wait() releases the lock.
Refer to JLS here.
From JLS
20.1.7 public final void wait(long millis)throws IllegalMonitorStateException, InterruptedException
.
.
This method causes the current thread (call it T) to place itself in the wait set (�17.14) for this object and then to relinquish any and all synchronization claims on this object
.
.
.
You can get more info in JLS Threads chapter also.
regds
maha anna

[This message has been edited by maha anna (edited April 23, 2000).]
Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
Maha,
Your explanation is very good regarding wait, notify. But can you give us one sample program which illustrates the wait,notify and notifyall? We would like to see your example to demonstrate how any object gets lock and others are notified etc etc...
Thanks & expecting your reply
Regds,
Thiru.


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
 
Don't get me started about those stupid light bulbs.
 
subject: wait()