Difference Between ReentrantLock and Synchronized in Java

JavaObject Oriented ProgrammingProgramming

There are two ways to get a lock on the shared resource by multiple threads. One is Reentrant Lock (Or ReadWriteLock ) and the other is by using the Synchronized method.

ReentrantLock class has been provided in Java concurrency package from Java 5. 

It is the implementation of Lock interface and According to java docs, implementation of Lock interface provides more extensive operation than can be obtained using synchronized method.

Sr. No.KeyReentrantLockSynchronized
Acquire Lock 
Reentrant lock class provides lock() methods to get a lock  on the shared resource by thread 
You need to just write synchronized keyword to acquire a lock  
Release Lock 
To release lock , programmers have to call unlock() method
It is done implicitly 
 Ability to interrupt
lockInterruptibly() method can be used to interrupt the thread  
There is no way to interrupt the thread
Constructor of this class has fairness parameter. If it is set to true then locks favor granting access to the longest-waiting
 * thread
Lock does not guarantee any particular  access orde
Lock Release Order 
Lock can be released in any order 
Lock should be released in the same order in which they were acquired 

Example of ReentrantLock

public class ReentrantLockExample implements Runnable{
   private Lock lock=new ReentrantLock();
   public void run() {
      try {
         //Lock some resource

      catch (InterruptedException e) {
      finally {

Example of SynchronizedLock

public class SynchronizedLockExample implements Runnable{
   public void run() {
      synchronized (resource) {
         //Lock some resource
Updated on 18-Nov-2019 07:06:01