File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested synchronized statements

 
Priyha Jootu
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Priyha Jootu
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Valentin for such a fast response.
regds,
priyha.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic