This week's book giveaway is in the Open Source forum.
We're giving away four copies of RabbitMQ in Depth and have Gavin Roy on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Want help... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of RabbitMQ in Depth this week in the Open Source forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Want help..." Watch "Want help..." New topic

Want help...

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Hi All,
Pls look at fallowing question,
public void getLocks(Object a, Object b)
//do something
and the following instantiations:
Object obj1 = new Object();
Object obj2 = new Object();
obj1 and obj2 are accesible to two different threads and the threads are about to call the getLocks() method.
Assume the first thread calls the method getLocks(obj1, obj2).
Which of the following is true?

Select 1 correct option.

a.The second thread should call getLocks(obj2, obj1)
b.The second thread should call getLocks(obj1, obj2)

c.The second thread should call getLocks() only after first thread exits out of it.
d.The second thread may call getLocks() any time and passing parameters in any order.

e.None of the above.

Ans given for this question is b.Pls anyone explain me.

--------------<br />Prashant<br />SCJP-91%
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Prashant
Asnwer A is wrong - It can cause deadlock...
Answer B is true - you can call 2 getLocks(obj1,obj2) methods.
Answer C is wrong - You can call them paralelly. It's just a paralel threads.
Answer D is wrong - You can call it any time but you cannot pass parameters in obj2,obj1 order (wrong answer A)
Answer E is wrong - B is true:-)
Jamal Hasanov
hiti hitox

Joined: Jan 23, 2003
Posts: 3
Answer 2 is right. A good method to avoid deadlocks is to block the involved objects always in the same order, because the 2. thread can only enter code (an so block obj1), when the lock of obj2 AND obj1 is released.
Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Thanks All.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
so... i have a question... why is there a synchronized block of code WITHIN a synchonized block of code; what is the use of this? does it become another entity accessible by a different thread or something?
whatever the reason for the nested synchronized code, i assume that once the inner synchonized code is done with obj2 then the outer synchronized code will be able to complete, and so therefore that indicates the order that the objects should be handed to the new thread?

giddee up
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Jasper, within the inner synchronized block two locks are hold. It is also possible for a thread to adquire multiple locks calling a synchronized method that in turn calls another synchronized method and so on.
The order in which this serie of locks must be adquired by several threads must be the same. Otherwise there is a risk of deadlock:
Suppose thread t1 calls getLocks(obj1, obj2) and progress untill it locks obj1 but not yet obj2. Now t2 calls getLocks(obj2, obj1) and progress locking on obj2 but it cannot lock on obj1 because t1 has locked it. Then there is a deadlock because t1 is blocked trying to adquire obj2 which is owned by t2. None thread will progress because both are waiting for the other to release the lock they own.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Want help...