• 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

regarding synchronized

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ranchers can any one explain this question
i confused about ans how it is b

Consider the following method:


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 accessible 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.


------------------------------------------------------------------
$ Velan Vel @ SCJP 1.4 $
You learn From Your Failures, Others Will Learn From Your Success
------------------------------------------------------------------
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Velan,

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.



Here say we are having Thread1 and Thread2.
Thread1 calls getLocks(obj1,obj2). That means it will lock the "obj1". And then will try aquiring lock on "obj2".

Now Consider the options,
a). If Thread2 calls it with (obj2,obj1) there will be deadlock.
b). Thread2 have to wait to obtain the lock
c). No chance(I can call anything at anytime, I'll get blocked if resource is locked.
d). refer a)'s answer. there can be a prob
e). leave it.

So according to logic "b" is the only satisfactory answer. I was wondering between "b" and "d". Looking for some good explanation for that.

hope this will help,

Regards,
Mausam
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be D (which implicitly covers A and D ie you can call getLocks method with any order and any time, JVM will take care of when to allow the method call to be executed)
 
Lakshmanan Arunachalam
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small correction in my above post,

read as

which implicitly covers A and B
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, the method has to be getLocks ( obj1, obj2 );

This is a typical situation of "dining phylosophers". What the code is doing is acquiring locking incrementally. If another thread is calling getLocks ( obj2, obj1 ) and obtains the lock on obj2, when at the same time the first thread has obtained a lock on obj1, a deadlock will occur. When multiple threads are competing for multiple resources, in order to prevent deadlock situation, always let them compete on the same resource.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, everyone is saying that the locks must be grabbed in the same order -- or deadlock will result. I think it would be easier to understand this if you understand how a deadlock will result.

Let's say thread 1 is trying to grab lock A then lock B. And let's say thread 2 is trying to grab lock B then lock A. And at the same time !!

Thread 1 grabs lock A. Thread 2 grabls lock B. Thread 1 tries to grab lock B, but it is busy so it will wait. Thread 2 tries to grab lock A, but it is busy so it will have to wait.

Lock A is held by thread 1 who is waiting for Lock B, which is held by thread 2 who is waiting for Lock A, which is held by thread 1 who is waiting for Lock B, which is held by thread 2 who is waiting for Lock A, which is held by thread 1 who is waiting for Lock B, which is held by thread 2 who is waiting for Lock A, which is held by thread 1 who is waiting for Lock B, which is held by thread 2 who is waiting for Lock A, ...

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic