Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Differences between the two types of Thread synchronization

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to K&B book,

synchronized blocks are better to use than synchronized methods. The reason give there is method level synchronization affects performance?

Please anyone can explain why method level synchronization is not preferred and how does it affect performance?

Also what is the difference between the method level synchronization and synchronized block?
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anu,

Method level synchronization is not appropriate because at times you need to attain the lock at a very fine level.
What i mean to say is that, may be you are calling method A and that method A is calling some other method called method B, just to do
some verification. At this point you will be needing to obtain the lock. So if you put the synchronization at the method
level, it will create a whole lot of delay because all other threads have to wait then. Rather then putting the synchronization
in this case on method level, just synchronize the region above the statement which is calling method B. In this case atleast
all other threads can do anything prior calling method B rather then waiting on method level. Method level synchronization
reduces performance as well.


Hope this helps,
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Anuradha,


Synchronized blocks are better to use than synchronized methods. The reason give there is method level synchronization affects performance?



This depends of the kind of code you run.

If your method has code that don't need to be synchronized, then it would make sense to use synchronized block as it gives finer grained control over what you synchronize. Let's say the same method that has lot's of code that don't need to be synchronized and you synchronize on method level, then it means that all the other threads will have to wait till currently executing thread finishes the method. On the other hand, if you synchronize only what is really needed using the synchronized block, then other threads can run the code before the synchronized block without needing to wait after other threads.

Note that there should be no performance differences between synchronized method and a synchronized block if the method contains only code that must be synchronized. For example these two methods should be equivalent in regards to performance and concurrency (similar ex in K&B on page 737):
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All good explanations above. But I will give an example.

Think about this analogy (this is my original thinking ). A house has a main door and it has several rooms inside it, including a bathroom. Assume 5 people live in this house and all have the authority to enter/use it. What if one person wants to use the bathroom? He goes inside the room and locks the door. But this still allows the other 4 people to use all other rooms as they wish. The only thing they can't use is the bathroom (assuming there is only one!). So this is like synchronizing a small block of the code.......... But now think about this...what will happen if he locks the entire house i.e. the main door and not allow anyone into the house, even though he wants to only use the bathroom? Is that necessary or efficient? NO. Why? Because he locked the entire house when he didn't have to!!! Now everyone else has to wait outside in the bad weather and they are so annoyed . This person could have only locked the bathroom and still allowed the other occupants to use all the other rooms. That would have been efficient!!! So here even though the entire house is shared, it is enough to lock only the bathroom when one wants to use it.

Synchronization is similar. Only synchronize the ABSOLUTE MINIMUM code you want and still allow other threads to execute the rest of the code, while one thread is executing the synchronized section. Otherwise it makes your code inefficient. Hope this makes it clear to you.
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all of you for the great reply.

Larry, your reply for very easy to understand.
 
reply
    Bookmark Topic Watch Topic
  • New Topic