IntelliJ Java IDE
The moose likes Threads and Synchronization and the fly likes differences between synchronized blocks and synchronized() method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "differences between synchronized blocks and synchronized() method" Watch "differences between synchronized blocks and synchronized() method" New topic
Author

differences between synchronized blocks and synchronized() method

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 200

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

Joined: Jan 28, 2003
Posts: 1900

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).


Steve
 
 
subject: differences between synchronized blocks and synchronized() method
 
Threads others viewed
Synchroniztaion question
Synchronizing Code
locks threads
locking an object other than this
unlock hangs
MyEclipse, The Clear Choice