aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question on wait() from K&B master exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "question on wait() from K&B master exam" Watch "question on wait() from K&B master exam" New topic
Author

question on wait() from K&B master exam

Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
Hi well since i m likely to take my exam on next tuesday, today i took the K&B master exam which comes on the accompanying CD.
Well i got only 2 ques wrong i.e. my score was 59/61. 1 was a foolish mistake but the other question is the one which i think was ambiguous.
the question was a to choose 2 correct statements which r true.
the ans were:
1.notifyAll() must be called from within synchronized code.
2.when the wait() method is called the thread releases all its locks.
well i got the 1st one right but I think the second answer given is wrong.
Bcoz when the wait method is called on an object, the thread releases only the lock for that object
please tell me if i m right or the ans given is right
Lakshmi Saradha
Ranch Hand

Joined: Oct 21, 2003
Posts: 170
But, what happens when a thread holding the lock on an object goes to the wait state? Is this possible?


Thanks,<br />Lakshmi.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
I believe i have heard Mohit statement somewhere before and i think he is correct with his assumption.
I am not sure about this but if the thead in the waiting pool still have locks for objects, then this will extremely increase the chance of a deadlock to happen. Am i right?
And on the other hand, what is the point of having the lock of an object if it is in the waiting pool?


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
I still think that Mohit is correct. Threads releases their locks on object when the wait() method is called.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Originally posted by Vicken Karaoghlanian:
I still think that Mohit is correct. Threads releases their locks on object when the wait() method is called.


Thread release the lock and enter wait state. Another thread acquires the lock. That thread has to invoke notify/notifyAll so that the first thread which is waiting gets a chance to execute.


Groovy
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Pradeep

Thread release the lock and enter wait state. Another thread acquires the lock. That thread has to invoke notify/notifyAll so that the first thread which is waiting gets a chance to execute.

This statement is correct, I totally agree with it. Dislike your old post which you deleted.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Dislike your old post which you deleted.

It has 3 years since I have actually used Threads in real life. Yes, I did study threads for SCJP but not much.
That is why Javaranch is great , nice way to brush various technologies and mistakes are all caught.
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by Pradeep

It has 3 years since I have actually used Threads in real life. Yes, I did study threads for SCJP but not much.
That is why Javaranch is great , nice way to brush various technologies and mistakes are all caught.


Cheers man....
[ December 08, 2003: Message edited by: Vicken Karaoghlanian ]
Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
hi well when the thread holds locks on multiple objects it only releases locks on the object on which the wait() method is called.
the ans given in the exam suggests that it releases all the locks. which is not correct as given in the book Core Java 2 Advanced topics by Cay Horstmann in Ch1
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
The given answer is wrong.

wait() method releases the lock on the current object only. If it has also acquired locks on some other objects, it does not release those locks.

[ December 08, 2003: Message edited by: Vicken Karaoghlanian ]
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

what happens when a thread holding the lock on an object goes to the wait state

Yes it is possible.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
I believe Mohit is right: a thread doesn't release locks on ALL objects if it goes into waiting state. Only the lock on the object wait() was called from would be released. Here's the code to solidify the concept:

This program never completes because the first running thread retains a lock on sb1 StringBuffer object even after being put in a waiting pool by the call to sb2.wait().
[ December 08, 2003: Message edited by: Vad Fogel ]
[ December 08, 2003: Message edited by: Vad Fogel ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on wait() from K&B master exam
 
Similar Threads
Threads
Question on a Mock Exam Question
using wait() method in Threads
Threads question.
Wait with timeout