• 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

Synchronized Block and Synchronized method

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

can anyone explain the difference between the synchronized block and a synchronized method and the advantage of using a synchronized block over a synchronized method ?
 
Ranch Hand
Posts: 76
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Block synchronization allows for a more fine grained synchronization. You can have, for example, a portion of a method, which is synchronized and the rest is not synchronized.
When the method is synchronized, it means that it is essentially the same as if there is one synchronization block which spans the whole method. For example:

is the same as

The same applies to static methods, but the synchronized block has a lock on MyClass.class, if the method is defined in class MyClass.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other advantage of the block approach is that it gives you control over exactly what you synchronize on. A synchronized method will lock on the object (or if static, the class). When you use a block it synchronizes on whatever object you specify.

For instance, your object may contain two collections, and you want to make sure access to each collection is synchronized, but separately (no reason to block two different threads accessing the different collections at the same time). In that case use blocks synchronized on the collections themselves.

Bottom line, using blocks gives you more control. Using the keyword on methods is more concise, so use it if that's all you need to do.
 
Ranch Hand
Posts: 62
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Before trying to know the advantages and disadvantages. Please help understand the fact that, "Synchronization" is REVERSE of "Concurrency". This means whatever code you make "Synchronized" will be executed "NON-CONCURRENTLY" by the threads.

"The problem at hand is to find the optimal solution so that MOST of the code remains unsynchronized but still thread safe."

This implies that one should reduce the scope of the synchronized code as much as possible but keeping the code thread safe.

Synchronized blocks are there for the rescue.


Synchronized block:

Advantage:

1. Provides more granularity. In the sense, you can reduce the scope of the synchronized code.
2. Provides flexibility. In the sense one can include/exclude the code from the synchronized block without affecting the method signature.

Disadvantage:

1. When you declare a method itself as synchronized, then just by looking at the method signature one can understand that it is synchronized as against the synchronized blocks.


Synchronized Methods:

Advantage:

1. When you declare a method itself as synchronized, then just by looking at the method signature one can understand that it is synchronized as against the synchronized blocks.


Disadvantage:

1. Provides less granularity.
2. Less flexible.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:When you use a block it synchronizes on whatever object you specify.


Good Point!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic