• 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

wait()

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Shiva.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why Cis not correct??
wait() throws InterruptedException.Am I right??
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice explanation Maha
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic