Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQ+ thread question

 
Yasir Qureshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yasir Qureshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3712
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic