Author | Topic: wait() |
Shiva ranch hand |
posted April 15, 2000 06:50 PM
Dr.Hunt's mock exam Q # 39
Thanks. ------------------ [This message has been edited by Shiva (edited April 15, 2000).]
|
maha anna bartender |
posted April 15, 2000 06:59 PM
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. [This message has been edited by maha anna (edited April 15, 2000).]
|
Shiva ranch hand |
posted April 15, 2000 08:54 PM
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 bartender |
posted April 15, 2000 09:51 PM
Thanks Shiva.
|
Alkesh Nandanwar ranch hand |
posted April 15, 2000 10:15 PM
Shiva, I think the given answer is correct In my poinion the current thread will not issue the notify() or notify all() call. Some other thread need to invoke notify() or notify all() call. Because wait(), notify() are instance methods of the Object class.
|
maha anna bartender |
posted April 15, 2000 11:28 PM
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 unregistered |
posted April 16, 2000 10:28 PM
Why Cis not correct?? wait() throws InterruptedException.Am I right??
|
Carol Willing greenhorn |
posted April 16, 2000 10:58 PM
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 bartender |
posted April 17, 2000 09:38 AM
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 unregistered |
posted April 17, 2000 11:49 PM
Nice explanation Maha
|
Betty Reynolds ranch hand |
posted April 18, 2000 01:10 AM
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 |
posted April 23, 2000 09:43 PM
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 bartender |
posted April 23, 2000 10:14 PM
Yes. Wait() releases the lock. Refer to JLS here. From JLS You can get more info in JLS Threads chapter also. regds [This message has been edited by maha anna (edited April 23, 2000).]
|
| | |