aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Urgent q regarding wait() for Bert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Urgent q regarding wait() for Bert" Watch "Urgent q regarding wait() for Bert" New topic

Urgent q regarding wait() for Bert

Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
well this is a question from Java2 Exam Notes
Q. Which is true of a thread that is blocked inside a wait() method call?

A. The thread holds an object lock
B. The thread does not hold an object lock
C. The thread might hold an object lock
The ans given is B. According to me the answer should be C. Consider this code:
public void aMethod(){
b.wait(); //lock on object b held by thread is released
// but the lock on object a is still held by thread

}catch(InterruptedException ie){}
So for the exam what shud we answer B or C because C is the right answer but if u don't consider the fact that a thread can hold multiple locks then ans B is correct.
Shahid Mithani

Joined: Dec 04, 2003
Posts: 8
I know this question is for Bert, but I felt like answering it. (I was raising my hand and going Ask me! Ask me!)
I would think, the waiting thread will give up all the locks, not just the one on which it is waiting. So answer B is what I would think as the right answer.
Sagarika nair
Ranch Hand

Joined: Aug 13, 2003
Posts: 39
I think Mohit is right.When a thread calls wait on a particular object ,it releases the lock for that object only(i.e the object on which it called wait on) but it can still hold the lock of other objects.
The question is quite tricky that way.
So it doesn't release all its locks.
But ya I guess as far as the exam is concerned, Bert should tell us how to deal with such a question.
Waiting for the reply .
Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
Why is this urgent?

SCWCD: Online Course, 50,000+ words and 200+ questions
Bert Bates

Joined: Oct 14, 2002
Posts: 8886
Hi guys,
I'm really flattered, but I can't take the pressure! You're killing me man . This is a wonderful open forum - and everybody should answer when they can - answering questions on these forums is one of the BEST ways to study. When you stick your neck out and answer a question on this forum you really tend to make sure you're right before posting! (Also, putting what you know into words helps your brain lock in the knowledge.)
Here's another reason that it's problematic to ask a particular person for an answer (notice how I'm continuing to NOT answer the question ?), you can never be sure that a particular person will be around! For instance, I've been locked away ALL week, so this is really my first time at the ranch for almost 6 days - not a very good response time for URGENT issues. (plus, URGENT issues on JavaRanch tend to bug me; we're all volunteers, and everybody's posts are important! ).
So here's my advice:
1 - Post well thought out, detailed questions, and give as much context as possible.
2 - Use other people's questions as a great learning opportunity! This forum is about studying for the certification, and it seems that the people who do best on the exam, are the people who contribute the most. Answering questions on this forum is a GREAT way to learn.
Mohit, ask everyone for their help, and let's all get an interesting 'threads' discussion going
[ December 13, 2003: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Don Wood
Ranch Hand

Joined: Dec 05, 2003
Posts: 65
I think that you are correct. When a thread goes into a wait the object that was used to call wait() is unlocked but if any other locks are held, they remain locked.
The api definition for wait() has the following statement:

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

By the way, itis not a good idea to hold locks while sleeping as that can do very bad things to your performance.
Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
I'm still curious as to why this issue was urgent, if there is something particularly urgent in this subject I feel I should be aware, again why was this urgent?
Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hi Marcus,
i think it is urgent as Mohit is to give the SCJP soon, prob. in this week itself,

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
I agree. Here's the link:
subject: Urgent q regarding wait() for Bert