Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization

 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
ok now i got it..Thanks Ankit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic