File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent q regarding wait() for Bert

 
Mohit Goyal
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(){
synchronized(a){
synchronized(b){
//code
try{
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
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this urgent?
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bert
[ December 13, 2003: Message edited by: Bert Bates ]
 
Don Wood
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit,
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
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marcus,
i think it is urgent as Mohit is to give the SCJP soon, prob. in this week itself,
Bye,
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic