Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an advantage to use a Synchronized Method instead of a Synchronized Block?

Can any one tell me the advantage of synchronized method over synchronized block with an example?

like image 508
Warrior Avatar asked Feb 22 '09 03:02

Warrior


People also ask

Which is better synchronized block or synchronized method?

To acquire a lock on an object for a specific set of code block, synchronized blocks are the best fit. As a block is sufficient, using a synchronized method will be a waste. More specifically with Synchronized Block , it is possible to define the object reference on which are want to acquire a lock.

Why we use synchronized method?

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

Why might we use a synchronized block?

A Synchronized block is a piece of code that can be used to perform synchronization on any specific resource of the method. A Synchronized block is used to lock an object for any shared resource and the scope of a synchronized block is smaller than the synchronized method.

Is it better to make whole getInstance () method synchronized or just critical section is enough which one you will prefer?

Which one you will prefer? Answer : This is again related to double checked locking pattern, well synchronization is costly and when you apply this on whole method than call to getInstance() will be synchronized and contented.


1 Answers

Can anyone tell me the advantage of the synchronized method over the synchronized block with an example? Thanks.

There is not a clear advantage of using synchronized method over the block.

Perhaps the only one ( but I wouldn't call it an advantage ) is you don't need to include the object reference this.

Method:

public synchronized void method() { // blocks "this" from here....      ...     ...     ... } // to here 

Block:

public void method() {      synchronized( this ) { // blocks "this" from here ....          ....         ....         ....     }  // to here... } 

See? No advantage at all.

Blocks do have advantages over methods though, mostly in flexibility because you can use another object as lock whereas syncing the method would lock the entire object.

Compare:

// locks the whole object ...  private synchronized void someInputRelatedWork() {     ...  } private synchronized void someOutputRelatedWork() {     ...  } 

vs.

// Using specific locks Object inputLock = new Object(); Object outputLock = new Object();  private void someInputRelatedWork() {     synchronized(inputLock) {          ...      }  } private void someOutputRelatedWork() {     synchronized(outputLock) {          ...      } } 

Also if the method grows you can still keep the synchronized section separated:

 private void method() {      ... code here      ... code here      ... code here     synchronized( lock ) {          ... very few lines of code here     }      ... code here      ... code here      ... code here      ... code here } 
like image 135
OscarRyz Avatar answered Sep 16 '22 13:09

OscarRyz