7 Difference Between Synchronized Method And Synchronized Block

SHARE

Synchronized Method & Synchronized Block

Synchronization is the ability to control the access of multiple threads to share resources. Without synchronization, it is possible for one thread to modify a shared resource while another thread is in the process of using or updating that resource.

There are two synchronization syntax in Java Language.  The practical differences are in controlling scope and the monitor. With a synchronized method, the lock is obtained for the duration of the entire method. With synchronized blocks you can specify exactly when the lock is needed.

Basically, synchronized blocks are more general and synchronized methods can be rewritten to use synchronized blocks. So if you want to lock the whole object, use a synchronized method. If you want to keep other parts of the object accessible to other threads, use synchronized block.

If you choose the locked object carefully, synchronized blocks will lead to less contention, because the whole object/class is not blocked. This applies similarly to static methods, a synchronized static method will acquire a lock in the whole class object, while a synchronized block inside a static method will acquire a lock in the object between parentheses.

The main difference between synchronized block and synchronized method is that synchronized block locks the code within the block whereas synchronized method locks the entire object.

What You Need To Know About Synchronized Method

  • Synchronized method acquires a lock on the whole object. This means no other thread can use any synchronized method in the whole object while the method is being run by one thread.
  • A synchronized method uses the method receiver as a lock, ‘this’ for non static methods and the enclosing class for static methods.
  • Synchronized method always locks either on current object represented by class level lock, if its static synchronized method.
  • In case of synchronized method, lock is acquired by thread when it enter method and released when it leaves method, either normally or by throwing Exception.
  • For synchronized methods, the lock will be held throughout the method scope.
  • A synchronized static method will acquire a lock in the whole class object.

What You Need To Know About Synchronized Block.

  • Synchronized blocks acquire a lock in object between parentheses after the synchronized keyword. Meaning no other thread can acquire a lock on the locked object until the synchronized block exits.
  • Synchronized blocks use the expression as a lock.
  • Synchronized block provide granular control over lock, as you can use arbitrary any lock to provide mutual exclusion to critical section code.
  • In case of synchronized block, thread acquires lock when they enter synchronized block and released when they leave synchronized block.
  • Synchronized block can throw NullPointerException if the expression provided as parameter evaluates to null, which is not the case with synchronized methods.  
  • In the synchronized block, the lock is held only during that block scope also referred to as critical section.
  • A synchronized block inside a static method will acquire a lock in the object between parentheses.

Difference Between Synchronized Method And  Block In Tabular Form

SYNCHRONIZED METHOD SYNCHRONIZED BLOCK
Synchronized method acquires a lock on the whole object. This means no other thread can use any synchronized method in the whole object while the method is being run by one thread.   Synchronized blocks acquire a lock in object between parentheses after the synchronized keyword. Meaning no other thread can acquire a lock on the locked object until the synchronized block exits.  
A synchronized method uses the method receiver as a lock, ‘this’ for non static methods and the enclosing class for static methods.   Synchronized blocks use the expression as a lock.  
Synchronized method always locks either on current object represented by class level lock, if its static synchronized method.   Synchronized block provide granular control over lock, as you can use arbitrary any lock to provide mutual exclusion to critical section code.  
In case of synchronized method, lock is acquired by thread when it enter method and released when it leaves method, either normally or by throwing Exception.   In case of synchronized block, thread acquires lock when they enter synchronized block and released when they leave synchronized block.  
For synchronized methods, the lock will be held throughout the method scope.   In the synchronized block, the lock is held only during that block scope also referred to as critical section.  
A synchronized static method will acquire a lock in the whole class object.   A synchronized block inside a static method will acquire a lock in the object between parentheses.