Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

differences between synchronized blocks and synchronized() method  RSS feed

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I want to know the technical differences between synchronized blocks and synchronized() method other than scope, about scope I am clear on that...!! please explain the other technical differences in detail..!!

secondly I just want to know in context of lock also..since when I was going through differences between synchronized blocks and synchronized() method..I read that when you synchronize a method the object that is used to invoke that method is the object lock that must be acquired but when you synchronize a block of code you must specify object lock you want to use as the lock..? I didn't understand this ..please explain this in detail..!!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You ask
Saral Saxena wrote:I want to know the technical differences between synchronized blocks and synchronized() method other than scope, about scope I am clear on that...!! please explain the other technical differences in detail..!!

Then you say:
I read that when you synchronize a method the object that is used to invoke that method is the object lock that must be acquired but when you synchronize a block of code you must specify object lock you want to use as the lock..?

This pretty much sums up the difference between a synchronized method and a synchronized block. For all intents and purposes, these two code blocks are the same:




I didn't understand this ..please explain this in detail..!!


When you use the synchronized keyword, you provide an Object which acts like a gate keeper to the synchronized code it protects. This Object is called the Object Lock. Only one thread is allowed to execute any code protected by that Object at a time. If multiple threads try to pass a synchronized portion of code (method or block) they must obtain the Object Lock, and if some other thread already has that lock, then any other thread must wait for it to let go of the Lock before they can continue to work.

A synchronized method always uses the instance of the class it is being executed on as the 'gatekeeper'. If you have two Objects of the same type (MyObject obj1 and MyObject obj2) then each will use their own 'this' reference as the gatekeeper. It saves you from having to type in those extra lines of code I added to the second code example I provided. You can also synchronize static methods, but since there is no instance to act as the Object Lock it uses the Class object (MyObject.class).

When you synchronize a block, rather than a method, you have to specify an Object to use as the Object Lock, none is provided automatically for you. This provides more granularity in that you can protect smaller sections of code (since synchronized code is a threading bottleneck) and can protect code based on external Objects. For example if you wanted to modify a List in multiple threads, you probably want to synchronize on the List itself, since that List may be being modified in different locations by different Objects (and since it could be modified by different Objects, a synchronized method would not protect the List from being modified by multiple threads at once, since each Object would have its own instance used for the synchronized method lock).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!