aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread synchronize question from JQ+ test 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 "Thread synchronize question from JQ+ test" Watch "Thread synchronize question from JQ+ test" New topic
Author

Thread synchronize question from JQ+ test

Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
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

Joined: Dec 18, 2001
Posts: 417
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

Joined: Jul 22, 2000
Posts: 9047
    
  10

If d) were the correct answer, you could pass in the arguments like a) suggests and end up with a deadlock situation.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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

Joined: Nov 20, 2001
Posts: 47
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

Joined: Jan 11, 2002
Posts: 203
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?

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread synchronize question from JQ+ test