File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes nested synchronized statements 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 "nested synchronized statements" Watch "nested synchronized statements" New topic
Author

nested synchronized statements

Priyha Jootu
Greenhorn

Joined: Sep 20, 2001
Posts: 24
Hi All,
I have copied from JLS below:
A single thread may hold a lock more than once.
The example:
class Test {
public static void main(String[] args) {
Test t = new Test();
synchronized(t) {
synchronized(t) {
System.out.println("made it!");
}
}
}
}
prints:
made it!
This example would deadlock if a single thread were not permitted to lock a lock more than once.
1. I am unable to understand what is said above. How does a deadlock occur here?
2. Why does a thread need more than one lock on a single object/multiple objects?
3. In what situations a thread need more than 1 lock ?
please explain.
Thanks,
Priyha.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
There is no deadlock here. All that the JLS wants to say here is that monitors are reentrant, which means that a thread owning an object's lock may call another synchronized method of that object without waiting (because the lock is his).
nesting two synchronized block on the same object does not buy you anything as far as I know. This is just to demonstrate that once you have an object's lock it is yours and you may "acquire" it one more time although you acquire it, in fact, just one time !
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Priyha Jootu
Greenhorn

Joined: Sep 20, 2001
Posts: 24
Thanks Valentin for your explanation and CONGRATS for becoming bartender.
I have another question:
Lets say there is an class C and it has 3 synchronized methods namely S1, S2, S3 and 3 threads running T1, T2, T3.
Lets say that thread T1 calls the synchronized method S1, so it acquires the lock of that object. Now,is it possible for the other threads T2 and T3 access the other sync. methods S2 and S3 at the same time?
Please explain.
Thanks,
Priyha.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
given your description only one thread may own an object lock at a given time, so if a thread executes a synchronized method (i.e. it acquired the object's lock and still owns it) then other threads wanting to execute another synchronized method will be blocked until the object's lock is released. This is the whole point of synchronization !
BUT, other threads may execute unsynchronized method of class C at anytime since they do not have to acquire the object's lock to execute those methods...
HIH

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
Priyha Jootu
Greenhorn

Joined: Sep 20, 2001
Posts: 24
Thank you very much Valentin for such a fast response.
regds,
priyha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nested synchronized statements