• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

nested synchronized statements

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Valentin for such a fast response.
regds,
priyha.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic