Forums Register Login

synchronized

+Pie Number of slices to send: Send
#1 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 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?


Options

Select 1 correct option




1 The second thread should call getLocks(obj2, obj1)
2 The second thread should call getLocks(obj1, obj2)
3 The second thread should call getLocks() only after first thread exits out of it
4 The second thread may call getLocks() any time and passing parameters in any order
5 None of the above
+Pie Number of slices to send: Send
What do you think the answer is? Why? Where is this question from?

We have a couple policies here, please ShowSomeEffort and please QuoteYourSources.
+Pie Number of slices to send: Send
I was solving a mock test of SCJP which was available online...i got this question from there...
I gave answer as option 1 and it was right....

but i don't know the explanation...please explain...

thanks in advance
+Pie Number of slices to send: Send
 

curve karve wrote:I was solving a mock test of SCJP which was available online...i got this question from there...
I gave answer as option 1 and it was right....

but i don't know the explanation...please explain...

thanks in advance



According to me option 2 is right. Option 1 can lead to deadlock.
+Pie Number of slices to send: Send
I agree with Rajiv.
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
What about you answer these questions first.

W. Joe Smith wrote:What do you think the answer is? Why? Where is this question from?

We have a couple policies here, please ShowSomeEffort and please QuoteYourSources.



And read the FAQ's including this one.
+Pie Number of slices to send: Send
Sorry for my previous post, answer 2 is probably correct

Martin
+Pie Number of slices to send: Send
Maggie when you post a question from a mock exam, you have to Quote Your Sources exactly, saying "some mock exam" is not enough. If you don't provide the exact source, I might have to delete this topic itself, so please let us know from where you got the question...
Power corrupts. Absolute power xxxxxxxxxxxxxxxx is kinda neat.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1086 times.
Similar Threads
Synchronization
Thread Doubt
Thread synchronize question from JQ+ test
Threads,locks problem
Want help...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:38:06.