aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Synchronized Block and Synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Synchronized Block and Synchronized method" Watch "Synchronized Block and Synchronized method" New topic
Author

Synchronized Block and Synchronized method

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

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 ?
Matthew Raw
Ranch Hand

Joined: Nov 30, 2010
Posts: 76

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

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.
Hrishikesh Yeshwant Alshi
Ranch Hand

Joined: Dec 06, 2010
Posts: 62


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.


Write your code as if the person who maintains it is a homicidal maniac who knows where you live.
OCPJP6/SCJP6 86%
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Good Point!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized Block and Synchronized method