Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JQ+ thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JQ+ thread question" Watch "JQ+ thread question" New topic
Author

JQ+ thread question

Yasir Qureshi
Ranch Hand

Joined: Apr 27, 2001
Posts: 44
Question ID :988386106781
Consider the following method:
public void getLocks(Object a, Object b)
{
synchronized(a)
{
sunchronized(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?
a)The 2nd thread should call getLocks(obj2,obj1)
b)The 2nd thread should call getLocks(obj1,obj2)
c)The 2nd thread may call function in any order
d)The 2nd thread should call getLocks when 1st thread exits
e)None of above
The correct answer is A , i think it should be C can any 1 explain thanks in advance.
Yasir Sufyan Qureshi
Farhan Tariq
Ranch Hand

Joined: May 14, 2001
Posts: 54
Hi Yasir!
Yeah I think I can tell u the reason. First thing, u did'nt synchronized the method getlocks(). So it is possible that more than one threads enter into this method. In the program first synchronized statement locks the object "a" which is the first parameter to method. So there is lock on object and if you call method with same order you can't get the execution.
wacth this:
suppose two objects
-------------------
Object A, B
getLocks(A,B) //A gets lock due to Synchronized(A)
so B is free and call to getlocks as
getLocks(B,A) //B gets lock due to Synchronized(A) which is actually synchronized(B), start execution for both because synchronization takes place on objects not the parameter no.
Hope this clear u
Regards
Farhan
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
The correct answer is B (as shown in the s/w). Second thread should not call getLocks(obj2, obj1) because this may result in a deadlock. Example: First threads gets the lock for obj1 and before it could get the lock for obj2, the second thread runs and gets the lock for obj2. Now, both the threads cannot proceed because thread1 needs the lock for obj2 which is held by thread2 and thread2 needs the lock for obj1 which is held by thread1.
So, the second thread should also call getLocks(obj1, obj2).
Obviously, if B is correct then C cannot be correct.
HTH,
Paul.
------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Yasir Qureshi
Ranch Hand

Joined: Apr 27, 2001
Posts: 44

Thanks Paul but according to your explanation, When first threads gets lock of Obj1 after a while it needs lock on Obj2 but I think when it needs lock of Obj2 and dint find it it goes to waiting state and thereby releasing lock of Obj1 which is free for Thread2 so how come the dead lock occurs.
I am not getting it.

(Sorry for mistake correct choic is B as mentioned by Paul)
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
>I think when it needs lock of Obj2 and dint find it it goes to waiting state and thereby releasing lock of Obj1
Why would it release the lock for obj1 ??? That lock won't be released until the thread exits out of the outer synch. block. And it can't exit out of outer synch. block till it finishes the inner synch. block... for which it needs obj2's lock.
-Paul.
------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
Yasir Qureshi
Ranch Hand

Joined: Apr 27, 2001
Posts: 44
But... when Thread wants a lock and it doent get it goes to waiting state and therbey releasing the lock it already have ? isnt it correct , can a thread have two locks at the same time?
Yasir
william shen
Ranch Hand

Joined: Jun 07, 2001
Posts: 60
Paul:
I agree with you!the right answer shoud be B! This is a typical deadlock problem!
Originally posted by Paul Anil:
The correct answer is B (as shown in the s/w). Second thread should not call getLocks(obj2, obj1) because this [b]may result in a deadlock. Example: First threads gets the lock for obj1 and before it could get the lock for obj2, the second thread runs and gets the lock for obj2. Now, both the threads cannot proceed because thread1 needs the lock for obj2 which is held by thread2 and thread2 needs the lock for obj1 which is held by thread1.
So, the second thread should also call getLocks(obj1, obj2).
Obviously, if B is correct then C cannot be correct.
HTH,
Paul.
[/B]

------------------

[This message has been edited by william shen (edited June 10, 2001).]
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3285
    
    7
>isnt it correct
No.
>can a thread have two locks at the same time?
Yes.
-Paul.

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JQ+ thread question