aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized" Watch "synchronized" New topic
Author

synchronized

maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
#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

Joined: Feb 10, 2009
Posts: 710
What do you think the answer is? Why? Where is this question from?

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


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
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

Joined: Dec 19, 2008
Posts: 62
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.


Rajeev Kumar
SCJP 6.0 Certified 96%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I agree with Rajiv.


SCJP 1.6 96%
maggie karve
Ranch Hand

Joined: Dec 29, 2009
Posts: 186
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Martin Sergeant
Greenhorn

Joined: May 20, 2003
Posts: 24
Sorry for my previous post, answer 2 is probably correct

Martin
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized