• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread synchronize question from JQ+ test

 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code and explanations:
------------------------------------
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).
-------------------------------------------------
Question: Which of the following is true?
a) The second Thread should call getLocks(obj2,obj1)
b) The second Thread should call getLocks(obj1,obj2)
c) The second Thread should call getLocks() only after the first exits out of it.
d) The second Thread my call getLocks() at any time and with passing arguments in any order
e) none of the above
-------------------------------------------------
The correct answer is b).
Why is that? I expected d) to be the correct answer.
-Bernd
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code sounds awkward though.
first thing is that who so ever calls the method getLocks would have to call it with correct parameters (obj1,obj2)
second the method is not synch though inside it objects are. so method can be accessed by both threads at the same time.
so with these 2 reasons answer b sounds ok.

Originally posted by Bernd Stransky:
Consider the following code and explanations:
------------------------------------
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).
-------------------------------------------------
Question: Which of the following is true?
a) The second Thread should call getLocks(obj2,obj1)
b) The second Thread should call getLocks(obj1,obj2)
c) The second Thread should call getLocks() only after the first exits out of it.
d) The second Thread my call getLocks() at any time and with passing arguments in any order
e) none of the above
-------------------------------------------------
The correct answer is b).
Why is that? I expected d) to be the correct answer.
-Bernd
 
Marilyn de Queiroz
Sheriff
Posts: 9062
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If d) were the correct answer, you could pass in the arguments like a) suggests and end up with a deadlock situation.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so I'm clear on how the deadlock could occur if d is the answer...
What could happen is obj1 enters block synchronized(a) and before it gets to synchronized(b), obj2 enters synchronized(a) so now both obj1 and obj2 are in synchronized code blocks and thus neither of them can access the synchronized(b) block since both objects are "stuck" in synchronized(a).
I probably didn't phrase that correctly but is that the type of deadlock that could possibly occur?

}
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick Reumann:
Just so I'm clear on how the deadlock could occur if d is the answer...
What could happen is obj1 enters block synchronized(a) and before it gets to synchronized(b), obj2 enters synchronized(a) so now both obj1 and obj2 are in synchronized code blocks and thus neither of them can access the synchronized(b) block since both objects are "stuck" in synchronized(a).
I probably didn't phrase that correctly but is that the type of deadlock that could possibly occur?

}

Hi,
yes that make sense, thanks all for the explanations.
Bernd
 
Shivaji Marathe
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick :
It's not the objects themselves executing the synchronized methods or codes. It is other threads that execute the synchronized method.
If a thread - let's say T1 calls the getlocks method, it will acquire a lock on object A and execute some code. During this time no other thread can acuire a lock on A. Any other thread calling getlocks method will be blocked until thread T1 is finished with the block of code that is synchronized on object A.
HTH

Originally posted by Rick Reumann:
Just so I'm clear on how the deadlock could occur if d is the answer...
What could happen is obj1 enters block synchronized(a) and before it gets to synchronized(b), obj2 enters synchronized(a) so now both obj1 and obj2 are in synchronized code blocks and thus neither of them can access the synchronized(b) block since both objects are "stuck" in synchronized(a).
I probably didn't phrase that correctly but is that the type of deadlock that could possibly occur?

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic