• 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

Synchronization

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have few doubts in Synchronization.

1)When we enter a synchronized non-static method we acquire a lock for an object.Does that mean the entire object is locked like both synchronized and non-synchronized methods?and what does object lock exactly mean?

2)Does a call to a non-synchronized method from a synchronized method make the calledmethod synchronized? for example



does getName method become sychronized?



Thanks
Vinay.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinu Menon wrote:I have few doubts in Synchronization.

1)When we enter a synchronized non-static method we acquire a lock for an object.Does that mean the entire object is locked like both synchronized and non-synchronized methods?and what does object lock exactly mean?



When you call a synchronized non-static method, the current thread acquires a lock on the object with which the method was called. This means that now no other thread will be able to execute synchronized non-static methods on that object. Other threads can however execute non-synchronized methods on the object. So the lock basically means that on that object no other thread can now execute synchronized non-static methods unless the current thread releases the lock either by calling wait or end of execution of the synchronized non-static method...
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinu Menon wrote:Does a call to a non-synchronized method from a synchronized method make the calledmethod synchronized? for example



does getName method become sychronized?



No getName will not be synchronized. Other threads can still call getName on the same object with which doStuff was called by some other thread. But remember that when getName is called from doStuff, then the thread that called doStuff still holds the lock on the object with which doStuff was called. The lock is not released when getName is called as doStuff has not completed. So you can call other synchronized methods from getName. This is what I mean

 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok now i got it..Thanks Ankit.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic