Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

synchronized

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Rajiv.
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for my previous post, answer 2 is probably correct

Martin
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic