• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want help...

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Pls look at fallowing question,
public void getLocks(Object a, Object b)
{
synchronized(a)
{
synchronized(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.
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:-)
Regadrs,
Jamal Hasanov
www.j-think.com
 
hiti hitox
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic