aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object lock Question 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 "Object lock Question" Watch "Object lock Question" New topic
Author

Object lock Question

Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
I am a bit confused about how synchronization takes place with respect to Object locks.
Consider the following code...
public class Test extends Thread
{
static Object obj1 = new Object();
static Object obj2 = new Object();
public void m1()
{
synchronized(obj1)
{
System.out.print("1 ");
synchronized(obj2)
{
System.out.println("2");
}
}
}
public void m2()
{
synchronized(obj2)
{
System.out.print("2 ");
synchronized(obj1)
{
System.out.println("1");
}
}
}

public void run()
{
m1();
m2();
}
public static void main(String[] args)
{
new Test().start();
new Test().start();
}
}
The question says there are 2 correct answers -
The choices are :-
a) It may result in a deadlock and the program may get stuck
b) No deadlock will occur
c) It may result in a deadlock by JVM will not let the program go into one & the program will not get stuck
d) The program will always print 1 2,2 1 & 1 2, 2 1.
e) Nothing can be said for sure.
------------------------------------
Now, this one has me going in a tizzy. I feel my concepts about how sychronization works on another object's lock aren't quite clear. Can someone pls. help.
thanks.


Himanshu Jhamb<br />SCJP2 v1.2 (April 2002)<br />SCJP2 v1.4 (May 2002)
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
I think that, the correct answer is A.
The reason of deadlock is mutual object locking.
If you want understand the behaviour of this code, then take paper&pencil and begin to vitually debugging of this code. For examle: Draw Obj1 & Obj2 circles and then start threads, execute their run() method (in paper i mean).
This diagram solution will help you in exam.
Thanx,
Jamal Hasanov
SCJP
www.j-think.com
imran anwar
Greenhorn

Joined: May 06, 2002
Posts: 12
i compiled and ran this code. and it works fine. i think the answers are b and d.
correct me if i am wrong.
rama pulavarthi
Greenhorn

Joined: May 12, 2002
Posts: 1
we cannot say for sure that deadlock will occur.
It may occur sometimes depending on the runtime.
But the above code can lead to deadlock.
It's my first post.
luco zhao
Ranch Hand

Joined: Apr 23, 2002
Posts: 50
Originally posted by Jamal Hasanov:
[QB]I think that, the correct answer is A.
The reason of deadlock is mutual object locking.
If you want understand the behaviour of this code, then take paper&pencil and begin to vitually debugging of this code. For examle: Draw Obj1 & Obj2 circles and then start threads, execute their run() method (in paper i mean).
This diagram solution will help you in exam.

That is a freshy idea, but could you give more details on this thread, such as how to drawing the diagrams? Thanks.


Great thanks,<br />Luco Zhao
luco zhao
Ranch Hand

Joined: Apr 23, 2002
Posts: 50
One thing more, could someone give scenarios of dead lock except for "mutual object locking", I am always weak on this point.
Himanshu Jhamb
Ranch Hand

Joined: Aug 01, 2001
Posts: 134
Hi.
Thanks for all your replies, but can someone pls. give a detailed response as to how this works ?
I mean, how does it work when locks are acquired on some other objects other than 'this'.
thanks
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
I'm no expert at threads but I'll try to answer your question about the locks.
Ok, lets say in your class you have an object called counter. If you have a synchronized block like:
synchronized(this)
{
...
}
Then the lock is on this, and since the object that you are worried about is a memeber of the class it will be "covered" by the lock.
But if the counter object is the only thing you are worried about then you can do
synchronized(counter)
{
...
}
Because that will ensure that only one Thread will access that counter object while in that code block.
This explination may be kinda weird, but maybe it will help you.


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
This scenario shold be recognized as a possible dealock. Its true that maybe it doesn't ends up in one. But there is a chance of happenning the following:
first thread executes m1 and gets obj1 lock, then it's preempted buy thread two, who tries to executes (now, before executed m1) method2 locking obj2, it cannot progress further because obj1 is locked by thread one. But when thread one is given CPU placet again, it cannot adquire the lock for obj2 because is already owned by thread two. Thus both threads are waiting for each other to release the lock, but this is not going to happen because they are locked to adquire the other's lock.
This is easy example because when several locks are to be adquired in a row, they must be in the same order. This will avoid the risk of deadlock.
This is a question that could appear in the exam.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object lock Question