Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized

 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#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
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think the answer is? Why? Where is this question from?

We have a couple policies here, please ShowSomeEffort and please QuoteYourSources.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rajiv Chopra
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Rajiv.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Sergeant
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my previous post, answer 2 is probably correct

Martin
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic