March 4, 2024

Synchronized Vs ReentrantLock in Java

The traditional way of acquiring a mutual exclusion lock in Java is to use synchronized keyword but Java 5 added new lock implementations like ReentrantLock and ReentrantReadWriteLock which provide extended locking operations capabilities. In this post we'll see the difference between synchronized and ReentrantLock in Java.

ReentrantLock Vs synchronized in Java

1- When you use synchronized keyword the implicit lock associated with the object is acquired automatically as soon as the synchronized method or block is entered and the lock is released automatically as soon as the synchronized method or block ends.

With ReentrantLock the acquisition and releasing the lock is done using the methods lock() and unlock().

2- The usage of synchronized is more rigid. All lock acquisition and release should occur in a block-structured way- when multiple locks are acquired they must be released in the opposite order, not doing that may result in a deadlock. All locks must be released in the same lexical scope in which they were acquired.

ReentrantLock usage is more flexible. It allows a lock to be acquired and released in different scopes, it also allows multiple locks to be acquired and released in any order.

3- The flexibility provided by ReentrantLock in terms of ordering of locks and the use of methods lock() and unlock() to acquire and release the lock puts the responsibility on user of following the convention as shown below when using ReentrantLock.

 Lock l = new ReentrantLock();
 l.lock();
 try {
   // access the resource protected by this lock
 } finally {
   l.unlock();
 }

lock should be acquired before entering a try block and lock should be released in a finally block.

While using synchronized to guard the critical section there is no such convention as acquiring and releasing of the lock is done implicitly.

4- ReentrantLock provides additional functionality over the use of synchronized methods and statements.

  • Provides a non-blocking attempt to acquire a lock using tryLock() method that acquires the lock only if it is not held by another thread at the time of invocation.
  • Provides a feature to acquire the lock that can be interrupted using lockInterruptibly() method that acquires the lock unless the current thread is interrupted.
  • Provides a feature to acquire the lock that can timeout using the tryLock(long timeout, TimeUnit unit) method that acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been interrupted.

5- ReentrantLock also provides an option for fairness which is not there with synchronized methods and statements. If using synchronized keyword any of the waiting thread can acquire the lock which may lead to thread starvation.

ReentrantLock class has a constructor which takes boolean value as an argument.

ReentrantLock(boolean fair)

when boolean value is passed as true this lock should use a fair ordering policy. Note that fair locks favors those threads that have been waiting the longest.

That's all for the topic Synchronized Vs ReentrantLock in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment