• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Want help...

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All.
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic